File tree Expand file tree Collapse file tree 6 files changed +290
-0
lines changed 
s3398_smallest_substring_with_identical_characters_i 
s3399_smallest_substring_with_identical_characters_ii 
s3398_smallest_substring_with_identical_characters_i 
s3399_smallest_substring_with_identical_characters_ii Expand file tree Collapse file tree 6 files changed +290
-0
lines changed Original file line number Diff line number Diff line change 1+ package  g3301_3400 .s3398_smallest_substring_with_identical_characters_i ;
2+ 
3+ // #Hard #2024_12_24_Time_1_ms_(100.00%)_Space_42.9_MB_(39.83%) 
4+ 
5+ public  class  Solution  {
6+     public  int  minLength (String  s , int  ops ) {
7+         char [] arr2  = s .toCharArray ();
8+         int  q  = '0' ;
9+         int  w  = '1' ;
10+         int  p1  = ops ;
11+         int  p2  = ops ;
12+         for  (int  i  = 0 ; i  < s .length (); i ++) {
13+             if  (arr2 [i ] != q ) {
14+                 p1 --;
15+             }
16+             if  (arr2 [i ] != w ) {
17+                 p2 --;
18+             }
19+             if  (q  == '0' ) {
20+                 q  = '1' ;
21+             } else  {
22+                 q  = '0' ;
23+             }
24+             if  (w  == '0' ) {
25+                 w  = '1' ;
26+             } else  {
27+                 w  = '0' ;
28+             }
29+         }
30+         if  (p1  >= 0  || p2  >= 0 ) {
31+             return  1 ;
32+         }
33+         int  low  = 2 ;
34+         int  high  = s .length ();
35+         int  ans  = 0 ;
36+         int  n  = s .length ();
37+         while  (low  <= high ) {
38+             int  mid  = (low  + high ) / 2 ;
39+             char [] arr  = s .toCharArray ();
40+             int  p  = ops ;
41+             int  c  = 1 ;
42+             for  (int  i  = 1 ; i  < n ; i ++) {
43+                 if  (arr [i ] == arr [i  - 1 ]) {
44+                     c ++;
45+                 } else  {
46+                     c  = 1 ;
47+                 }
48+                 if  (c  > mid ) {
49+                     if  (arr [i  - 1 ] == '0' ) {
50+                         arr [i  - 1 ] = '1' ;
51+                     } else  {
52+                         arr [i  - 1 ] = '0' ;
53+                     }
54+                     p --;
55+                     c  = 0 ;
56+                 }
57+             }
58+             if  (p  < 0 ) {
59+                 low  = mid  + 1 ;
60+             } else  {
61+                 ans  = mid ;
62+                 high  = mid  - 1 ;
63+             }
64+         }
65+         return  ans ;
66+     }
67+ }
Original file line number Diff line number Diff line change 1+ 3398\.  Smallest Substring With Identical Characters I
2+ 
3+ Hard
4+ 
5+ You are given a binary string ` s `  of length ` n `  and an integer ` numOps ` .
6+ 
7+ You are allowed to perform the following operation on ` s `  ** at most**  ` numOps `  times:
8+ 
9+ *    Select any index ` i `  (where ` 0 <= i < n ` ) and ** flip**  ` s[i] ` . If ` s[i] == '1' ` , change ` s[i] `  to ` '0' `  and vice versa.
10+ 
11+ You need to ** minimize**  the length of the ** longest**  substring of ` s `  such that all the characters in the substring are ** identical** .
12+ 
13+ Return the ** minimum**  length after the operations.
14+ 
15+ ** Example 1:** 
16+ 
17+ ** Input:**  s = "000001", numOps = 1
18+ 
19+ ** Output:**  2
20+ 
21+ ** Explanation:** 
22+ 
23+ By changing ` s[2] `  to ` '1' ` , ` s `  becomes ` "001001" ` . The longest substrings with identical characters are ` s[0..1] `  and ` s[3..4] ` .
24+ 
25+ ** Example 2:** 
26+ 
27+ ** Input:**  s = "0000", numOps = 2
28+ 
29+ ** Output:**  1
30+ 
31+ ** Explanation:** 
32+ 
33+ By changing ` s[0] `  and ` s[2] `  to ` '1' ` , ` s `  becomes ` "1010" ` .
34+ 
35+ ** Example 3:** 
36+ 
37+ ** Input:**  s = "0101", numOps = 0
38+ 
39+ ** Output:**  1
40+ 
41+ ** Constraints:** 
42+ 
43+ *    ` 1 <= n == s.length <= 1000 ` 
44+ *    ` s `  consists only of ` '0' `  and ` '1' ` .
45+ *    ` 0 <= numOps <= n ` 
Original file line number Diff line number Diff line change 1+ package  g3301_3400 .s3399_smallest_substring_with_identical_characters_ii ;
2+ 
3+ // #Hard #2024_12_24_Time_11_ms_(100.00%)_Space_45.7_MB_(54.55%) 
4+ 
5+ import  java .util .ArrayList ;
6+ import  java .util .List ;
7+ 
8+ public  class  Solution  {
9+     public  int  minLength (String  s , int  numOps ) {
10+         int  l  = s .length ();
11+         int  lingyi  = 0 ;
12+         int  yiling  = 0 ;
13+         List <Integer > pq  = new  ArrayList <>();
14+         char  thisone  = s .charAt (0 );
15+         int  chang  = 1 ;
16+         if  (thisone  == '0' ) {
17+             yiling ++;
18+         } else  {
19+             lingyi ++;
20+         }
21+         for  (int  i  = 1 ; i  < l ; i ++) {
22+             char  cur  = s .charAt (i );
23+             if  (cur  == thisone ) {
24+                 chang ++;
25+             } else  {
26+                 if  (chang  >= 2 ) {
27+                     pq .add (chang );
28+                 }
29+                 chang  = 1 ;
30+                 thisone  = cur ;
31+             }
32+             if  (i  % 2  == 0 ) {
33+                 if  (cur  == '0' ) {
34+                     yiling ++;
35+                 } else  {
36+                     lingyi ++;
37+                 }
38+             } else  {
39+                 if  (cur  == '0' ) {
40+                     lingyi ++;
41+                 } else  {
42+                     yiling ++;
43+                 }
44+             }
45+         }
46+         if  (numOps  >= lingyi  || numOps  >= yiling ) {
47+             return  1 ;
48+         }
49+         if  (chang  >= 2 ) {
50+             pq .add (chang );
51+         }
52+         int  one  = -1 ;
53+         int  two  = -1 ;
54+         for  (int  cur  : pq ) {
55+             if  (cur  > one ) {
56+                 two  = one ;
57+                 one  = cur ;
58+             } else  if  (cur  > two ) {
59+                 two  = cur ;
60+             }
61+         }
62+         if  (two  == -1 ) {
63+             return  one  / (numOps  + 1 ) > 1  ? one  / (numOps  + 1 ) : 2 ;
64+         }
65+         if  (numOps  == 0 ) {
66+             return  one ;
67+         }
68+         if  (numOps  == 1 ) {
69+             return  (one  / 2  > two ) ? (one  / 2  == 1  ? 2  : one  / 2 ) : two ;
70+         }
71+         int  left  = 2 ;
72+         int  right  = l  / (numOps  + 1 );
73+         while  (left  < right ) {
74+             int  mid  = left  + (right  - left ) / 2 ;
75+             int  sum  = 0 ;
76+             for  (Integer  integer  : pq ) {
77+                 sum  += integer  / (mid  + 1 );
78+             }
79+             if  (sum  <= numOps ) {
80+                 right  = mid ;
81+             } else  {
82+                 left  = mid  + 1 ;
83+             }
84+         }
85+         return  left ;
86+     }
87+ }
Original file line number Diff line number Diff line change 1+ 3399\.  Smallest Substring With Identical Characters II
2+ 
3+ Hard
4+ 
5+ You are given a binary string ` s `  of length ` n `  and an integer ` numOps ` .
6+ 
7+ You are allowed to perform the following operation on ` s `  ** at most**  ` numOps `  times:
8+ 
9+ *    Select any index ` i `  (where ` 0 <= i < n ` ) and ** flip**  ` s[i] ` . If ` s[i] == '1' ` , change ` s[i] `  to ` '0' `  and vice versa.
10+ 
11+ You need to ** minimize**  the length of the ** longest**  substring of ` s `  such that all the characters in the substring are ** identical** .
12+ 
13+ Return the ** minimum**  length after the operations.
14+ 
15+ ** Example 1:** 
16+ 
17+ ** Input:**  s = "000001", numOps = 1
18+ 
19+ ** Output:**  2
20+ 
21+ ** Explanation:** 
22+ 
23+ By changing ` s[2] `  to ` '1' ` , ` s `  becomes ` "001001" ` . The longest substrings with identical characters are ` s[0..1] `  and ` s[3..4] ` .
24+ 
25+ ** Example 2:** 
26+ 
27+ ** Input:**  s = "0000", numOps = 2
28+ 
29+ ** Output:**  1
30+ 
31+ ** Explanation:** 
32+ 
33+ By changing ` s[0] `  and ` s[2] `  to ` '1' ` , ` s `  becomes ` "1010" ` .
34+ 
35+ ** Example 3:** 
36+ 
37+ ** Input:**  s = "0101", numOps = 0
38+ 
39+ ** Output:**  1
40+ 
41+ ** Constraints:** 
42+ 
43+ *    <code >1 <= n == s.length <= 10<sup >5</sup ></code >
44+ *    ` s `  consists only of ` '0' `  and ` '1' ` .
45+ *    ` 0 <= numOps <= n ` 
Original file line number Diff line number Diff line change 1+ package  g3301_3400 .s3398_smallest_substring_with_identical_characters_i ;
2+ 
3+ import  static  org .hamcrest .CoreMatchers .equalTo ;
4+ import  static  org .hamcrest .MatcherAssert .assertThat ;
5+ 
6+ import  org .junit .jupiter .api .Test ;
7+ 
8+ class  SolutionTest  {
9+     @ Test 
10+     void  minLength () {
11+         assertThat (new  Solution ().minLength ("000001" , 1 ), equalTo (1 ));
12+     }
13+ 
14+     @ Test 
15+     void  minLength2 () {
16+         assertThat (new  Solution ().minLength ("0000" , 2 ), equalTo (1 ));
17+     }
18+ 
19+     @ Test 
20+     void  minLength3 () {
21+         assertThat (new  Solution ().minLength ("0101" , 0 ), equalTo (1 ));
22+     }
23+ }
Original file line number Diff line number Diff line change 1+ package  g3301_3400 .s3399_smallest_substring_with_identical_characters_ii ;
2+ 
3+ import  static  org .hamcrest .CoreMatchers .equalTo ;
4+ import  static  org .hamcrest .MatcherAssert .assertThat ;
5+ 
6+ import  org .junit .jupiter .api .Test ;
7+ 
8+ class  SolutionTest  {
9+     @ Test 
10+     void  minLength () {
11+         assertThat (new  Solution ().minLength ("000001" , 1 ), equalTo (2 ));
12+     }
13+ 
14+     @ Test 
15+     void  minLength2 () {
16+         assertThat (new  Solution ().minLength ("0000" , 2 ), equalTo (1 ));
17+     }
18+ 
19+     @ Test 
20+     void  minLength3 () {
21+         assertThat (new  Solution ().minLength ("0101" , 0 ), equalTo (1 ));
22+     }
23+ }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments