-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathLongestPalindromeSubString.java
More file actions
55 lines (43 loc) · 1.1 KB
/
LongestPalindromeSubString.java
File metadata and controls
55 lines (43 loc) · 1.1 KB
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
48
49
50
51
52
53
54
55
package String;
/**
* Author - archit.s
* Date - 05/10/18
* Time - 12:57 PM
*/
public class LongestPalindromeSubString {
public String longestPalindrome(String A) {
int start = 0;
int maxLen = 1;
if(A.length()==1){
return A;
}
for(int i=1;i<A.length();i++){
//even length
int low = i-1;
int high = i;
while(low>=0 && high < A.length() && A.charAt(low) == A.charAt(high)){
low--;
high++;
}
low++;
high--;
if(high-low+1 > maxLen){
start = low;
maxLen = high-low +1;
}
low = i-1;
high = i+1;
while(low>=0 && high < A.length() && A.charAt(low) == A.charAt(high)){
low--;
high++;
}
low++;
high--;
if(high-low+1 > maxLen){
start = low;
maxLen = high-low +1;
}
}
return A.substring(start, start + maxLen);
}
}