Introduction
Palindrome-related problems are among the most common interview questions because they combine string manipulation, hashing, and optimization techniques.
In this problem, we are given an array of strings and must determine whether there exists a pair of different indices (i, j) such that:
arr[i] + arr[j]forms a palindrome.
The challenge is to find such a pair efficiently without checking every possible combination.
Problem Statement
Given an array of strings:
arr[]Determine whether there exists a pair of indices:
i ≠ jsuch that:
arr[i] + arr[j]is a palindrome.
Return:
trueif such a pair exists; otherwise, return:
falseExample 1
Input
["geekf", "geeks", "or", "keeg", "abc", "bc"]Pair Found
geekf + keeg
=
geekfkeegReverse:
geekfkeegSame forward and backward.
Output
trueExample 2
Input
["abc", "xyxcba", "geekst", "or", "bc"]Pair Found
abc + xyxcba
=
abcxyxcbaThis is a palindrome.
Output
trueExample 3
Input
["aa"]Only one string exists.
No valid pair:
i ≠ jcannot be satisfied.
Output
falseNaive Approach
A straightforward solution is:
Check every pair
(i, j).Concatenate strings.
Verify whether the result is a palindrome.
Pseudocode
for every i
for every j
if i != j
check arr[i] + arr[j]Complexity
O(n² × l)where:
n = number of stringsl = string length
For:
n = 20000this becomes too slow.
Key Observation
Suppose:
word = "abc"Reverse:
"cba"If another word equals:
"cba"then:
abc + cbabecomes:
abccbawhich is a palindrome.
This suggests storing strings in a hash map for quick reverse lookups.
An Even Better Observation
Consider:
word = "abcd"Split at every position.
Split 1
"" | abcdSplit 2
a | bcdSplit 3
ab | cdSplit 4
abc | dSplit 5
abcd | ""For every split, we examine:
Left Part
Right Part
We check whether one side is already a palindrome.
If yes, we only need to find the reverse of the other side.
Why This Works
Suppose:
word = "abc"Split:
a | bcLeft part:
"a"is already a palindrome.
If the reverse of:
"bc"which is:
"cb"exists in the array, then:
cb + abcforms a palindrome.
HashMap Optimization
Store every string in:
Map<String,Integer> mapExample:
abc → 0
cba → 1
xyx → 2Now every reverse lookup becomes:
O(1)Algorithm
Step 1
Try every possible split.
left = word[0...i-1]
right = word[i...end]Step 2
If left is a palindrome:
Search for:
reverse(right)in the HashMap.
Step 3
If right is a palindrome:
Search for:
reverse(left)in the HashMap.
Step 4
If found and the index differs:
return trueStep 5
After checking all words:
return falseExample Walkthrough
Input
["abc","cba"]HashMap
abc → 0
cba → 1Processing
word = abcSplit:
abc | ""Right side:
""is a palindrome.
Reverse of left:
cbaexists.
Different index:
1 ≠ 0Return:
trueJava Solution
class Solution {
private boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right))
return false;
left++;
right--;
}
return true;
}
public boolean palindromePair(String[] arr) {
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
map.put(arr[i], i);
}
for (int i = 0; i < arr.length; i++) {
String word = arr[i];
for (int cut = 0; cut <= word.length(); cut++) {
String left = word.substring(0, cut);
String right = word.substring(cut);
// Case 1
if (isPalindrome(left)) {
String revRight =
new StringBuilder(right)
.reverse()
.toString();
Integer idx = map.get(revRight);
if (idx != null && idx != i) {
return true;
}
}
// Case 2
if (cut != word.length() &&
isPalindrome(right)) {
String revLeft =
new StringBuilder(left)
.reverse()
.toString();
Integer idx = map.get(revLeft);
if (idx != null && idx != i) {
return true;
}
}
}
}
return false;
}
}Dry Run
Input
["abc","cba"]HashMap
abc → 0
cba → 1Processing
abcSplit:
abc | ""Right:
""Palindrome:
YesReverse of left:
cbaFound in map:
Index = 1Different from current index:
1 != 0Return:
trueComplexity Analysis
Let:
n = number of strings
l = maximum string lengthTime Complexity
For every word:
l splitsFor each split:
Palindrome check = O(l)Total:
O(n × l²)Space Complexity
HashMap stores all strings:
O(n × l)Additional reverse strings and substrings:
O(n × l²)which matches the expected complexity.
Why This Solution Is Optimal
The brute-force solution compares every pair:
O(n²)which is impractical for:
n = 20000Using:
HashMap for reverse lookup
Palindrome prefix/suffix checking
reduces the complexity to:
O(n × l²)which is the expected solution for this problem.
Conclusion
The Palindrome Pairs problem is a classic interview question that combines:
String manipulation
Hashing
Palindrome checking
Prefix and suffix decomposition
The key insight is that for a concatenation to become a palindrome, one part must already be a palindrome while the reverse of the remaining part exists elsewhere in the array.
By using a HashMap and checking all possible splits of each word, we achieve an efficient solution with:
Time Complexity: O(n × l²)
Space Complexity: O(n × l²)making it suitable for large inputs and coding interviews.

Join the conversation! Your thoughts help the community grow.