Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
69 lines (59 loc) · 2.06 KB

File metadata and controls

69 lines (59 loc) · 2.06 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
68
69
package LeetCode.String;
import LeetCode.array.LeetCode64;
public class LeetCode647 {
private int count;
public int countSubstrings2(String s) {
if (s == null || s.length() == 0) return 0;
char[] arr = s.toCharArray();
for(int i = 0; i < s.length(); i++) {
expand(arr, i, i);
expand(arr, i, i+1);
}
return count;
}
private void expand(char[] arr, int l, int r) {
while(l >= 0 && r < arr.length && arr[l] == arr[r]){
count++;
l--;
r++;
}
}
// ===========================================
private char[] manacherStr(String s) {
char[] arr = s.toCharArray();
char[] manacherArr = new char[arr.length*2+1];
int index = 0;
for (int i = 0; i < manacherArr.length; i++) {
manacherArr[i] = (i & 1) == 0 ? '#' : arr[index++];
}
return manacherArr;
}
// 本想利用manacher算法来实现0(N)时间复杂度,但失败
public int countSubstrings(String s) {
if (s == null || s.length() == 0) return 0;
char[] arr = manacherStr(s);
int[] pArr = new int[arr.length];
int pr = 0, index=0, count = 0;
for (int i = 0; i < arr.length; i++) {
pArr[i] = i < pr ? Math.min(pArr[index*2-i], pr-i) : 1;
while (pArr[i]+i < arr.length && i-pArr[i] >= 0) {
if (arr[pArr[i]+i] == arr[i-pArr[i]]) {
pArr[i]++;
}else {
break;
}
}
if (pArr[i]+i > pr) {
pr = pArr[i] + i;
index = i;
}
// if ( ((i & 1) == 1 && pArr[i] > 2) || ((i & 1) == 0 && pArr[i] > 1)) count++;
}
return count + s.length();
}
public static void main(String[] args) {
LeetCode647 instance = new LeetCode647();
String s = "aaaaa";
System.out.println(instance.countSubstrings(s));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.