Skip to content

Commit f1a2cf3

Browse files
committed
Runtime: 17 ms (Top 5.9%) | Memory: 43.57 MB (Top 5.0%)
1 parent da87baa commit f1a2cf3

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
1+
// Runtime: 17 ms (Top 5.9%) | Memory: 43.57 MB (Top 5.0%)
2+
13
class Solution {
24
public List<Integer> partitionLabels(String s) {
3-
Map<Character, Integer> map = new HashMap<>();
4-
// filling impact of character's
5-
for(int i = 0; i < s.length(); i++){
6-
char ch = s.charAt(i);
7-
map.put(ch, i);
8-
}
9-
// making of result
10-
List<Integer> res = new ArrayList<>();
11-
int prev = -1;
12-
int max = 0;
135

14-
for(int i = 0; i < s.length(); i++){
15-
char ch = s.charAt(i);
16-
max = Math.max(max, map.get(ch));
17-
if(max == i){
18-
// partition time
19-
res.add(max - prev);
20-
prev = max;
21-
}
22-
}
23-
return res;
6+
List<Integer>lr=new ArrayList<>();
7+
8+
HashMap<Character,Boolean>mp=new HashMap<>();
9+
10+
int count=0;
11+
12+
for(int i=0;i<s.length();i++){
13+
14+
if(!mp.containsKey(s.charAt(i))&&s.lastIndexOf(Character.toString(s.charAt(i)))!=i){
15+
mp.put(s.charAt(i),true);
2416
}
25-
}
17+
else if(mp.containsKey(s.charAt(i))&&s.lastIndexOf(Character.toString(s.charAt(i)))==i){
18+
19+
mp.remove(s.charAt(i));
20+
21+
}
22+
23+
if(mp.isEmpty()){
24+
lr.add(count+1);
25+
count=0;
26+
}
27+
else{
28+
count++;
29+
}
30+
}
31+
32+
33+
return lr;
34+
35+
36+
}
37+
}

0 commit comments

Comments
 (0)