import
java.util.*;
public
class
Main
{
static
boolean
ispalin(String word)
{
if
(word.length() ==
1
|| word.length() ==
0
) {
return
true
;
}
int
l =
0
;
int
r = word.length() -
1
;
while
(l <= r) {
if
(word.charAt(l) != word.charAt(r)) {
return
false
;
}
l++;
r--;
}
return
true
;
}
static
Vector<String> palindromicPairs(String[] words)
{
Vector<String> output =
new
Vector<String>();
if
(words.length ==
0
||
words.length ==
1
)
{
return
output;
}
HashMap<String, Integer> mp =
new
HashMap<>();
for
(
int
i =
0
; i < words.length; i++) {
mp.put(words[i], i);
}
for
(
int
i =
0
; i < words.length; i++)
{
if
((words[i]).length() ==
0
)
{
for
(Map.Entry<String, Integer> key : mp.entrySet())
{
if
(key.getValue() == i)
{
continue
;
}
if
(ispalin(key.getKey()))
{
output.add(key.getKey());
}
}
}
for
(
int
j =
0
; j < words[i].length(); j++)
{
String s1 = words[i].substring(
0
, j +
1
);
String s2 = words[i].substring(j +
1
);
if
(ispalin(s1))
{
StringBuffer arr =
new
StringBuffer(s2);
arr.reverse();
s2 =
new
String(arr);
String temp = s2;
if
(mp.containsKey(s2) && mp.get(s2) != i)
{
String ans = s2 + words[i];
output.add(ans);
}
s2 = temp;
}
if
(ispalin(s2))
{
String temp = s1;
StringBuffer arr =
new
StringBuffer(s1);
arr.reverse();
s1 =
new
String(arr);
if
(mp.containsKey(s1) && mp.get(s1) != i)
{
String ans = words[i] + s1;
output.add(ans);
}
s1 = temp;
}
}
}
return
output;
}
public
static
void
main(String[] args) {
String[] words = {
"geekf"
,
"geeks"
,
"or"
,
"keeg"
,
"abc"
,
"ba"
};
Vector<String> result = palindromicPairs(words);
for
(String x : result)
System.out.println(x);
}
}