VOOZH about

URL: https://www.geeksforgeeks.org/dsa/palindrome-pair-in-an-array-of-words-or-strings/

⇱ Palindrome Pairs - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Palindrome Pairs

Last Updated : 5 May, 2026

Given an array arr[] consisting of n strings. The task is to find if there exists a pair of indices (i, j) such that i ≠ j and the concatenation arr[i] + arr[j] forms a palindrome.
Return true if such a pair exists; otherwise, return false.

Note: A string is considered a palindrome if it reads the same forward and backward.

Examples:

Input: arr[] = ["geekf", "geeks", "or", "keeg", "abc", "bc"]
Output: true
Explanation: Strings "geekf" and "keeg" can be concatenated to form a palindromic string "geekfkeeg" .

Input: arr[] = ["abc", "xyxcba", "geekst", "or", "keeg", "bc"]
Output: true
Explanation: Strings "abc" and "xyxcba" can be concatenated to form a palindromic string "abcxyxcba" .

Input: arr[] = ["abc", "ab", "xyz"]
Output: false
Explanation: No palindromic string can be formed by concatenating any two of the given strings.

[Naive Approach] Using Nested Loop - O(n^2 * k) Time and O(1) Space

The idea is to iteratively generate all possible pairs of strings using the nested loops and check if any of them is a palindrome.

Dry run for arr = ["geekf", "geeks", "or", "keeg", "abc", "bc"]:

  • For i = 0, j = 1 ---> "geekf" + "geeks" = "geekfgeeks" ---> not palindrome | "geeks" + "geekf" = "geeksgeekf" ---> not palindrome
  • For i = 0, j = 2 ---> "geekf" + "or" = "geekfor" ---> not palindrome | "or" + "geekf" = "orgeekf" ---> not palindrome
  • For i = 0, j = 3 ---> "geekf" + "keeg" = "geekfkeeg" ---> palindrome so return true.

Final answer : True


Output
True

Time Complexity: O(n* k), here n is the number of words in the array arr[] and k is the length of the longest concatenated string.
Auxiliary Space: O(1)

[Expected Approach] Using Trie Data Structure - O(n * k^2) Time and O(n * k) Space

The idea is to use Trie Data Structure to store all the strings and efficiently search for palindromic pairs. The intuition behind the Trie approach is to efficiently match each word with the reverse of another word to form a palindrome, without checking all pairs. We insert all words in reversed form into a Trie, while also storing information about prefixes that are palindromes. Then, for each word, we traverse the Trie to see if it can match with a reversed word such that the remaining part of the word forms a palindrome.


Output
True

[Alternate Approach] Using HashMap - O(n * k^2) Time and O(n * k) Space

The above approach can also be implemented using HashMap instead of Trie. The intuition behind this approach is to break each word into two parts (a prefix and a suffix) and try to form a palindrome by pairing it with another word. For every split, we check two cases: if the prefix is a palindrome, then we look for the reverse of the suffix in the array; and if the suffix is a palindrome, we look for the reverse of the prefix. If such a matching word exists, combining them will form a palindrome.

Dry run for arr = ["geekf", "geeks", "keeg"]:

  • We store reverse of each word with its index:
    "fkeeg" ---> 0
    "skeeg" ---> 1
    "geek" ---> 2
  • For i = 0 ---> word = "geekf"
    j = 0 --> left = "g", right = "eekf" --> left palindrome and "eekf" not in map so skip ; right not palindrome so skip
    j = 1 --> left = "ge", right = "ekf" --> left not palindrome so skip ; right not palindrome so skip
    j = 2 --> left = "gee", right = "kf" --> left not palindrome so skip ; right not palindrome so skip
    j = 3 --> left = "geek", right = "f" --> left not palindrome so skip ; right palindrome and "geek" found in map at index 2 ---> so return true.

Final answer : True


Output
True
Comment