/
_0647_PalindromicSubstrings.java
58 lines (54 loc) · 1.83 KB
/
_0647_PalindromicSubstrings.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
package com.diguage.algorithm.leetcode;
/**
* = 647. Palindromic Substrings
*
* https://leetcode.com/problems/palindromic-substrings/[Palindromic Substrings - LeetCode]
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-01-31 23:17
*/
public class _0647_PalindromicSubstrings {
/**
* Runtime: 1 ms, faster than 100.00% of Java online submissions for Palindromic Substrings.
* Memory Usage: 37.8 MB, less than 11.39% of Java online submissions for Palindromic Substrings.
*
* Copy from: https://leetcode-cn.com/problems/palindromic-substrings/solution/hui-wen-zi-chuan-by-leetcode/[回文子串 - 回文子串 - 力扣(LeetCode)]
*/
public int countSubstrings(String s) {
char[] A = new char[2 * s.length() + 3];
A[0] = '@';
A[1] = '#';
A[A.length - 1] = '$';
int j = 2;
for (char c : s.toCharArray()) {
A[j++] = c;
A[j++] = '#';
}
int[] Z = new int[A.length];
int center = 0, right = 0;
for (int i = 1; i < Z.length - 1; i++) {
if (i < right) {
Z[i] = Math.min(right - i, Z[2 * center - i]);
}
while (A[i + Z[i] + 1] == A[i - Z[i] - 1]) {
Z[i]++;
}
if (i + Z[i] > right) {
center = i;
right = i + Z[i];
}
}
int result = 0;
for (int i : Z) {
result += (i + 1) / 2;
}
return result;
}
public static void main(String[] args) {
_0647_PalindromicSubstrings solution = new _0647_PalindromicSubstrings();
int r1 = solution.countSubstrings("abc");
System.out.println((r1 == 3) + " : " + r1);
int r2 = solution.countSubstrings("aaa");
System.out.println((r2 == 6) + " : " + r2);
}
}