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 ≠ j
such that:
arr[i] + arr[j]
is a palindrome.
Return:
true
if such a pair exists; otherwise, return:
false
Example 1
Input
["geekf", "geeks", "or", "keeg", "abc", "bc"]
Pair Found
geekf + keeg
=
geekfkeeg
Reverse:
geekfkeeg
Same forward and backward.
Output
true
Example 2
Input
["abc", "xyxcba", "geekst", "or", "bc"]
Pair Found
abc + xyxcba
=
abcxyxcba
This is a palindrome.
Output
true
Example 3
Input
["aa"]
Only one string exists.
No valid pair:
i ≠ j
cannot be satisfied.
Output
false
Naive 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 = 20000
this becomes too slow.
Key Observation
Suppose:
word = "abc"
Reverse:
"cba"
If another word equals:
"cba"
then:
abc + cba
becomes:
abccba
which 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
"" | abcd
Split 2
a | bcd
Split 3
ab | cd
Split 4
abc | d
Split 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 | bc
Left part:
"a"
is already a palindrome.
If the reverse of:
"bc"
which is:
"cb"
exists in the array, then:
cb + abc
forms a palindrome.
HashMap Optimization
Store every string in:
Map<String,Integer> map
Example:
abc → 0
cba → 1
xyx → 2
Now every reverse lookup becomes:
O(1)
Algorithm
Step 1
Try every possible split.

Join the conversation! Your thoughts help the community grow.