forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongPalss.java
More file actions
39 lines (37 loc) · 937 Bytes
/
LongPalss.java
File metadata and controls
39 lines (37 loc) · 937 Bytes
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
import java.util.Scanner;
public class LongPalss {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
longestPalindromicSubString(str);
}
public static int max=0;
public static int start=0;
static void longestPalindromicSubString(String s) {
if(s.length()<2)
{
System.out.println(s);
return;
}
for(int i=0 ;i<s.length()-1;i++)
{
extendPalindrome(s,i,i);
extendPalindrome(s,i,i+1);
}
System.out.println(s.substring(start,start+max));
}
private static void extendPalindrome(String s,int i,int j)
{
while( j>=0 && i<s.length() && s.charAt(i)==s.charAt(j))
{
j--;
i++;
}
int windowSize=i-j-1;
if(max<windowSize)
{
start=j+1;
max=windowSize;
}
}
}