-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPartitonLebel.java
30 lines (25 loc) · 938 Bytes
/
PartitonLebel.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
class Solution {
public List<Integer> partitionLabels(String s) {
List<Integer> ans = new ArrayList<>();
int freq[] = new int[26];
for(int i=0; i<s.length(); i++) {
freq[s.charAt(i) - 'a'] = i;
}
int minp = -1, curAns = 0;
for(int i=0; i<s.length(); i++) {
int lp = freq[s.charAt(i) - 'a'];
// Here we will whatever comes a b c the one whose last pos is greator
// will be stored there so that if i will that pos then the cur substring is partition label
minp = Math.max(minp, lp);
if(i == minp) {
curAns++;
ans.add(curAns);
curAns = 0;
minp = -1;
} else {
curAns++;
}
}
return ans;
}
}