-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0214_ShortestPalindrome.java
More file actions
60 lines (55 loc) · 1.79 KB
/
P0214_ShortestPalindrome.java
File metadata and controls
60 lines (55 loc) · 1.79 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
56
57
58
59
60
package yyl.leetcode.p02;
import yyl.leetcode.util.Assert;
/**
* <h3>最短回文串</h3><br>
* 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。<br>
*
* <pre>
* 示例 1:
* 输入: "aacecaaa"
* 输出: "aaacecaaa"
*
* 示例 2:
* 输入: "abcd"
* 输出: "dcbabcd"
* </pre>
*/
public class P0214_ShortestPalindrome {
public static void main(String[] args) {
Solution solution = new Solution();
Assert.assertEquals("aaacecaaa", solution.shortestPalindrome("aacecaaa"));
Assert.assertEquals("dcbabcd", solution.shortestPalindrome("abcd"));
}
// 暴力法
// 找出字符串s中所有以s[0]为起点的回文字符串中最长的那一个回文字符串,那剩余的元素逆序拼接至开头即可。
// 时间复杂度:O(n^2)
// 空间复杂度:O(n)
static class Solution {
public String shortestPalindrome(String s) {
int n = s.length();
if (n <= 1) {
return s;
}
char[] chars = s.toCharArray();
int index = 0;
for (int i = 0; i < n; i++) {
if (isPalindrome(chars, 0, i)) {
index = i;
}
}
StringBuilder builder = new StringBuilder();
for (int i = n - 1; i > index; i--) {
builder.append(chars[i]);
}
return builder.append(s).toString();
}
private boolean isPalindrome(char[] chars, int left, int right) {
while (left < right) {
if (chars[left++] != chars[right--]) {
return false;
}
}
return true;
}
}
}