-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestPalindromicSubstring.java
41 lines (38 loc) · 1.13 KB
/
LongestPalindromicSubstring.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
package com.namanh.string;
/**
* https://leetcode.com/problems/longest-palindromic-substring
*/
public class LongestPalindromicSubstring {
String mString;
int length;
int maxLength = 1;
String result;
public String longestPalindrome(String s) {
mString = s;
result = "" + s.charAt(0);
length = s.length();
int index = 0;
while (index < length - 1) {
if (s.charAt(index) == s.charAt(index + 1)) {
getPalindromic(index - 1, index + 2);
}
if (index < length - 2 && s.charAt(index) == s.charAt(index + 2)) {
getPalindromic(index - 1, index + 3);
}
index++;
}
return result;
}
private void getPalindromic(int start, int end) {
int curLength = end - start - 1;
while (start >= 0 && end < length && mString.charAt(start) == mString.charAt(end)) {
start--;
end++;
curLength += 2;
}
if (curLength > maxLength) {
maxLength = curLength;
result = mString.substring(start + 1, end);
}
}
}