# First string from the given array whose reverse is also present in the same array

Given a string array str[], the task is to find the first string from the given array whose reverse is also present in the same array. If there is no such string then print -1.

Examples:

Input: str[] = {“geeks”, “for”, “skeeg”}
Output: geeks
“geeks” is the first string from the array whose reverse is also present in the array i.e. “skeeg”.

Input: str[] = {“there”, “you”, “are”}
Output: -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Traverse the array element by element and for every string, check whether there is any string that appears after the current string in the array and is equal to the reverse of it. If yes then print the current string else print -1 in the end.

Below is the implementation of the above approach:

## 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 teh 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 teh 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

 ` `

Output:

```geeks
```

Efficient Approach: O(n) approach. This approach requires a Hashmap to store words as traversed. As we traverse, if reverse of current word found in the map, then reversed word is the first occurrence that is the answer. If not found at the end of traversal, return -1.

Below is the implementation of the above approach:

 `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 revered 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 `

Output:

```some
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Hi, I am Chaitanya Sharma I am a full stack developer and a coding enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.