File tree 1 file changed +33
-21
lines changed
scripts/algorithms/P/Partition Labels
1 file changed +33
-21
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 17 ms (Top 5.9%) | Memory: 43.57 MB (Top 5.0%)
2
+
1
3
class Solution {
2
4
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 ;
13
5
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 ) ;
24
16
}
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
+ }
You can’t perform that action at this time.
0 commit comments