import
java.util.ArrayList;
import
java.util.Collections;
public
class
Main {
public
static
boolean
isPalindrome(String s)
{
return
s.equals(
new
StringBuilder(s).reverse().toString());
}
public
static
void
largestPalin(ArrayList<String> s)
{
ArrayList<String> newlist =
new
ArrayList<>();
for
(String word : s) {
if
(isPalindrome(word)) {
newlist.add(word);
}
}
Collections.sort(newlist,
(a, b) -> a.length() - b.length());
System.out.println(newlist.get(newlist.size() -
1
));
}
public
static
void
main(String[] args)
{
String str
=
"My name is ava and i love Geeksforgeeks"
;
ArrayList<String> words =
new
ArrayList<>();
StringBuilder word =
new
StringBuilder();
for
(
char
c : str.toCharArray()) {
if
(c ==
' '
) {
words.add(word.toString());
word.setLength(
0
);
}
else
{
word.append(c);
}
}
if
(word.length() >
0
) {
words.add(word.toString());
}
largestPalin(words);
}
}