-
Notifications
You must be signed in to change notification settings - Fork 57
/
LongestPalindrome.java
47 lines (45 loc) · 1018 Bytes
/
LongestPalindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static int ispalindrome(String s)
{
String reverse = new StringBuffer(s).reverse().toString();
if(s.equals(reverse))
{
return 1;
}
else
{
return 0;
}
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
{
while(t-->0)
{
String str=sc.next();
String ans="";
int max=0;
for(int i=0;i<str.length();i++)
{
for(int j=str.length();j>i;j--)
{
String pal = str.substring(i,j);
if(ispalindrome(pal)==1)
{
if((j-i)>max)
{
ans=pal;
max=j-i;
}
}
}
}
System.out.println(ans);
}
}
}
}