-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path467.java
More file actions
96 lines (92 loc) · 2.7 KB
/
467.java
File metadata and controls
96 lines (92 loc) · 2.7 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
__________________________________________________________________________________________________
sample 2 ms submission
class Solution {
public int findSubstringInWraproundString(String p) {
if(p==null||p.length()==0)return 0;
int res = 0;
int[] rec = new int[26];
int cnt = 1;
char[] ca = p.toCharArray();
int start = ca[0]-'a';
for(int i=1;i<ca.length;i++){
if((ca[i-1]=='z'&&ca[i]=='a')||(ca[i]==ca[i-1]+1)){//continue
cnt++;
}else{//stop sequence
while(cnt>0){
if(cnt>rec[start]){
res+=cnt-rec[start];
rec[start]=cnt;
}
cnt--;
if(start==25)start=0;
else start++;
}
cnt=1;
start=ca[i]-'a';
}
}
while(cnt>0){//last one sequence
if(cnt>rec[start]){
res+=cnt-rec[start];
rec[start]=cnt;
}
cnt--;
if(start==25)start=0;
else start++;
}
return res;
}
}
__________________________________________________________________________________________________
sample 3 ms submission
class Solution {
int[] chs;
public int findSubstringInWraproundString(String p) {
int[] lens = new int[26];
int N = p.length();
chs = new int[N];
for (int i = 0; i < N; i++) {
chs[i] = p.charAt(i)-'a';
}
int start = 0;
int cur = 0;
while (cur < N) {
start = cur;
while (cur+1 < N && (chs[cur]+1) % 26 == chs[cur+1]) {
cur++;
}
update(lens, start, cur);
cur++;
}
int total = 0;
for (int len: lens) {
total += len;
}
return total;
}
void update(int[] lens, int start, int end) {
for (int i = start; i <= end; i++) {
if (end-i+1 <= lens[chs[i]]) {
break;
}
lens[chs[i]] = end-i+1;
}
}
}
__________________________________________________________________________________________________
sample 34912 kb submission
class Solution {
public int findSubstringInWraproundString(String p) {
int[] cnt = new int[26];
int len = 0;
for(int i=0; i<p.length(); i++) {
if(i>0 && (p.charAt(i) == p.charAt(i-1) + 1 || p.charAt(i-1) - p.charAt(i) == 25)) {
len++;
} else {
len = 1;
}
cnt[p.charAt(i)-'a'] = Math.max(cnt[p.charAt(i)-'a'], len);
}
return IntStream.of(cnt).sum();
}
}