📜  给定数组中的第一个字符串,其反向也存在于同一数组中

📅  最后修改于: 2022-05-13 01:57:08.812000             🧑  作者: Mango

给定数组中的第一个字符串,其反向也存在于同一数组中

给定一个字符串数组str[] ,任务是从给定数组中找到第一个字符串,其反向也出现在同一个数组中。如果没有这样的字符串,则打印-1
例子:

方法:逐个元素遍历数组,对于每一个字符串,检查数组中当前字符串后面是否有字符串,并且等于它的倒数。如果是,则打印当前字符串,否则最后打印 -1。
下面是上述方法的实现:

C++
// CPP implementation of the approach
#include
using namespace std;
 
    // Function that returns true if s1
    // is equal to reverse of s2
    bool isReverseEqual(string s1, string s2)
    {
 
        // If both the strings differ in length
        if (s1.length() != s2.length())
            return false;
 
        int len = s1.length();
        for (int i = 0; i < len; i++)
 
            // In case of any character mismatch
            if (s1[i] != s2[len - i - 1])
                return false;
 
        return true;
    }
 
    // Function to return the first word whose
    // reverse is also present in the array
    string getWord(string str[], int n)
    {
 
        // Check every string
        for (int i = 0; i < n - 1; i++)
 
            // Pair with every other string
            // appearing after the current string
            for (int j = i + 1; j < n; j++)
 
                // If first string is equal to the
                // reverse of the second string
                if (isReverseEqual(str[i], str[j]))
                    return str[i];
 
        // No such string exists
        return "-1";
    }
 
    // Driver code
    int main()
    {
        string str[] = { "geeks", "for", "skeeg" };
         
        cout<<(getWord(str, 3));
    }
     
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach
class GFG {
 
    // Function that returns true if s1
    // is equal to reverse of s2
    static boolean isReverseEqual(String s1, String s2)
    {
 
        // If both the strings differ in length
        if (s1.length() != s2.length())
            return false;
 
        int len = s1.length();
        for (int i = 0; i < len; i++)
 
            // In case of any character mismatch
            if (s1.charAt(i) != s2.charAt(len - i - 1))
                return false;
 
        return true;
    }
 
    // Function to return the first word whose
    // reverse is also present in the array
    static String getWord(String str[], int n)
    {
 
        // Check every string
        for (int i = 0; i < n - 1; i++)
 
            // Pair with every other string
            // appearing after the current string
            for (int j = i + 1; j < n; j++)
 
                // If first string is equal to the
                // reverse of the second string
                if (isReverseEqual(str[i], str[j]))
                    return str[i];
 
        // No such string exists
        return "-1";
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str[] = { "geeks", "for", "skeeg" };
        int n = str.length;
 
        System.out.print(getWord(str, n));
    }
}


Python3
# Python implementation of the approach
 
# Function that returns true if s1
# is equal to reverse of s2
def isReverseEqual(s1, s2):
 
    # If both the strings differ in length
    if len(s1) != len(s2):
        return False
     
    l = len(s1)
 
    for i in range(l):
 
        # In case of any character mismatch
        if s1[i] != s2[l-i-1]:
            return False
    return True
 
# Function to return the first word whose
# reverse is also present in the array
def getWord(str, n):
 
    # Check every string
    for i in range(n-1):
 
        # Pair with every other string
        # appearing after the current string
        for j in range(i+1, n):
 
            # If first string is equal to the
            # reverse of the second string
            if (isReverseEqual(str[i], str[j])):
                return str[i]
     
    # No such string exists
    return "-1"
 
 
# Driver code
if __name__ == "__main__":
    str = ["geeks", "for", "skeeg"]
    print(getWord(str, 3))
 
# This code is contributed by
# sanjeev2552


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function that returns true if s1
    // is equal to reverse of s2
    static bool isReverseEqual(String s1, String s2)
    {
 
        // If both the strings differ in length
        if (s1.Length != s2.Length)
            return false;
 
        int len = s1.Length;
        for (int i = 0; i < len; i++)
 
            // In case of any character mismatch
            if (s1[i] != s2[len - i - 1])
                return false;
 
        return true;
    }
 
    // Function to return the first word whose
    // reverse is also present in the array
    static String getWord(String []str, int n)
    {
 
        // Check every string
        for (int i = 0; i < n - 1; i++)
 
            // Pair with every other string
            // appearing after the current string
            for (int j = i + 1; j < n; j++)
 
                // If first string is equal to the
                // reverse of the second string
                if (isReverseEqual(str[i], str[j]))
                    return str[i];
 
        // No such string exists
        return "-1";
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String []str = { "geeks", "for", "skeeg" };
        int n = str.Length;
 
        Console.Write(getWord(str, n));
    }
}
 
// This code has been contributed by 29AjayKumar


PHP


Javascript


C++
#include 
using namespace std;
 
// Method that returns first occurrence of reversed word.
string getReversed(string words[], int length)
{
   
  // Hashmap to store word as we traverse
  map reversedWordMap;
  for(int i = 0; i < length; i++)
  {
     
    string reversedString = words[i];
    reverse(reversedString.begin(),reversedString.end());
      
    // check if reversed word exists in map.
    if (reversedWordMap.find(reversedString) !=
        reversedWordMap.end() and reversedWordMap[reversedString])
      return reversedString;
    else
       
      // else put the word in map
      reversedWordMap[words[i]] = true;
  }
  return "-1";
}
   
// Driver code
int main()
{
    string words[] = {"some", "geeks", "emos", "for", "skeeg"};
    int length = sizeof(words) / sizeof(words[0]);
    cout << getReversed(words, length);
    return 0;
}
 
// This code is contributed by divyesh072019


Java
import java.util.HashMap;
import java.util.Map;
 
public class ReverseExist {
 
    // Driver Code
    public static void main(String[] args) {
        String[] words = {"some", "geeks", "emos", "for", "skeeg"};
        System.out.println(getReversed(words, words.length));
    }
 
    // Method that returns first occurrence of reversed word.
    private static String getReversed(String[] words, int length) {
         
        // Hashmap to store word as we traverse
        Map reversedWordMap = new HashMap<>();
 
        for (String word : words) {
            StringBuilder reverse = new StringBuilder(word);
            String reversed = reverse.reverse().toString();
             
            // check if reversed word exists in map.
            Boolean exists = reversedWordMap.get(reversed);
            if (exists != null && exists.booleanValue()) {
                return reversed;
            } else {
                // else put the word in map
                reversedWordMap.put(word, true);
            }
 
        }
        return "-1";
    }
}
// Contributed by srika21m


Python3
# Method that returns first occurrence of reversed word.
def getReversed(words, length):
   
  # Hashmap to store word as we traverse
  reversedWordMap = {}
  for word in words:
    reversedString = word[::-1]
     
    # check if reversed word exists in map.
    if (reversedString in reversedWordMap and reversedWordMap[reversedString]):
      return reversedString
    else:
       
      # else put the word in map
      reversedWordMap[word] = True
  return "-1"
 
# Driver Code
if __name__ == "__main__":
  words = ["some", "geeks", "emos", "for", "skeeg"]
  print(getReversed(words, len(words)))
 
  # This code is contributed by chitranayal


C#
using System;
using System.Collections.Generic;
class GFG
{
     
    // Method that returns first occurrence of reversed word.
    static string getReversed(string[] words, int length)
    {
        
      // Hashmap to store word as we traverse
      Dictionary reversedWordMap =
        new Dictionary();
      for(int i = 0; i < length; i++)
      {
          
        char[] reversedString = words[i].ToCharArray();
        Array.Reverse(reversedString);
           
        // check if reversed word exists in map.
        if (reversedWordMap.ContainsKey(new string(reversedString)) &&
            reversedWordMap[new string(reversedString)])
          return new string(reversedString);
        else
            
          // else put the word in map
          reversedWordMap[words[i]] = true;
      }
      return "-1";
    }
 
  // Driver code
  static void Main()
  {
    string[] words = {"some", "geeks", "emos", "for", "skeeg"};
    int length = words.Length;
    Console.Write(getReversed(words, length));
  }
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
geeks

高效方法: O(n) 方法。这种方法需要一个 Hashmap 来存储遍历过的单词。当我们遍历时,如果在地图中找到当前单词的反转,那么反转的单词是第一个出现的答案。如果在遍历结束时没有找到,则返回 -1。
下面是上述方法的实现:

C++

#include 
using namespace std;
 
// Method that returns first occurrence of reversed word.
string getReversed(string words[], int length)
{
   
  // Hashmap to store word as we traverse
  map reversedWordMap;
  for(int i = 0; i < length; i++)
  {
     
    string reversedString = words[i];
    reverse(reversedString.begin(),reversedString.end());
      
    // check if reversed word exists in map.
    if (reversedWordMap.find(reversedString) !=
        reversedWordMap.end() and reversedWordMap[reversedString])
      return reversedString;
    else
       
      // else put the word in map
      reversedWordMap[words[i]] = true;
  }
  return "-1";
}
   
// Driver code
int main()
{
    string words[] = {"some", "geeks", "emos", "for", "skeeg"};
    int length = sizeof(words) / sizeof(words[0]);
    cout << getReversed(words, length);
    return 0;
}
 
// This code is contributed by divyesh072019

Java

import java.util.HashMap;
import java.util.Map;
 
public class ReverseExist {
 
    // Driver Code
    public static void main(String[] args) {
        String[] words = {"some", "geeks", "emos", "for", "skeeg"};
        System.out.println(getReversed(words, words.length));
    }
 
    // Method that returns first occurrence of reversed word.
    private static String getReversed(String[] words, int length) {
         
        // Hashmap to store word as we traverse
        Map reversedWordMap = new HashMap<>();
 
        for (String word : words) {
            StringBuilder reverse = new StringBuilder(word);
            String reversed = reverse.reverse().toString();
             
            // check if reversed word exists in map.
            Boolean exists = reversedWordMap.get(reversed);
            if (exists != null && exists.booleanValue()) {
                return reversed;
            } else {
                // else put the word in map
                reversedWordMap.put(word, true);
            }
 
        }
        return "-1";
    }
}
// Contributed by srika21m

Python3

# Method that returns first occurrence of reversed word.
def getReversed(words, length):
   
  # Hashmap to store word as we traverse
  reversedWordMap = {}
  for word in words:
    reversedString = word[::-1]
     
    # check if reversed word exists in map.
    if (reversedString in reversedWordMap and reversedWordMap[reversedString]):
      return reversedString
    else:
       
      # else put the word in map
      reversedWordMap[word] = True
  return "-1"
 
# Driver Code
if __name__ == "__main__":
  words = ["some", "geeks", "emos", "for", "skeeg"]
  print(getReversed(words, len(words)))
 
  # This code is contributed by chitranayal

C#

using System;
using System.Collections.Generic;
class GFG
{
     
    // Method that returns first occurrence of reversed word.
    static string getReversed(string[] words, int length)
    {
        
      // Hashmap to store word as we traverse
      Dictionary reversedWordMap =
        new Dictionary();
      for(int i = 0; i < length; i++)
      {
          
        char[] reversedString = words[i].ToCharArray();
        Array.Reverse(reversedString);
           
        // check if reversed word exists in map.
        if (reversedWordMap.ContainsKey(new string(reversedString)) &&
            reversedWordMap[new string(reversedString)])
          return new string(reversedString);
        else
            
          // else put the word in map
          reversedWordMap[words[i]] = true;
      }
      return "-1";
    }
 
  // Driver code
  static void Main()
  {
    string[] words = {"some", "geeks", "emos", "for", "skeeg"};
    int length = words.Length;
    Console.Write(getReversed(words, length));
  }
}
 
// This code is contributed by divyeshrabadiya07

Javascript


输出:
some