-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestPal.java
More file actions
29 lines (27 loc) · 907 Bytes
/
LongestPal.java
File metadata and controls
29 lines (27 loc) · 907 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
public class LongestPal {
static int maxLen = 0;
static String longestPalindrome = "";
public static String LongestPalindrome(String s){
for(int i = 0; i < s.length(); i++){
expandAroundCenter(s, i, i);
expandAroundCenter(s, i, i+1);
}
return longestPalindrome;
}
public static void expandAroundCenter(String s, int left, int right){
while(left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){
int currLen = right - left + 1;
if (currLen > maxLen){
maxLen = currLen;
longestPalindrome = s.substring(left, right + 1);
}
left--;
right++;
}
}
public static void main(String[] args) {
String s = "racecar123321";
String res = LongestPalindrome(s);
System.out.println(res);
}
}