-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestPalindromicSubstring.java
67 lines (61 loc) · 1.75 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package jpa.example.demo.algo;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
/**
* Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
*
* Example 1:
*
* Input: "babad"
* Output: "bab"
* Note: "aba" is also a valid answer.
* Example 2:
*
* Input: "cbbd"
* Output: "bb"
*
* ???
* "abcda" -> "a"
*/
public class LongestPalindromicSubstring {
static class Solution {
/**
* brute-force
*/
public String longestPalindrome(String s) {
if (s.length() == 1) return s;
String result = "";
for (int i = 0; i < s.length(); i++) {
for (int j = i; j <= s.length(); j++) {
if (i == j) continue;
String substring = s.substring(i, j);
if (isPalindromic(substring)) {
if (result.length() < substring.length()) {
result = substring;
}
}
}
}
return result;
}
private boolean isPalindromic(String str) {
int left = 0, right = str.length() - 1;
while (left <= right) {
if (str.charAt(left++) != str.charAt(right--)) return false;
}
return true;
}
}
public static void main(String[] args) {
String s1 = "babad";
String s2 = "cbbd";
String s3 = "a";
String s4 = "";
Arrays.asList(s1, s2, s3, s4)
.forEach(s -> {
String result = new Solution().longestPalindrome(s);
System.out.println(result);
});
}
}