File tree Expand file tree Collapse file tree 6 files changed +285
-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 +285
-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+ class Solution {
6+ fun minLength (s : String , ops : Int ): Int {
7+ val arr2 = s.toCharArray()
8+ var q = ' 0' .code
9+ var w = ' 1' .code
10+ var p1 = ops
11+ var p2 = ops
12+ for (i in 0 .. < s.length) {
13+ if (arr2[i].code != q) {
14+ p1--
15+ }
16+ if (arr2[i].code != w) {
17+ p2--
18+ }
19+ if (q == ' 0' .code) {
20+ q = ' 1' .code
21+ } else {
22+ q = ' 0' .code
23+ }
24+ if (w == ' 0' .code) {
25+ w = ' 1' .code
26+ } else {
27+ w = ' 0' .code
28+ }
29+ }
30+ if (p1 >= 0 || p2 >= 0 ) {
31+ return 1
32+ }
33+ var low = 2
34+ var high = s.length
35+ var ans = 0
36+ val n = s.length
37+ while (low <= high) {
38+ val mid = (low + high) / 2
39+ val arr = s.toCharArray()
40+ var p = ops
41+ var c = 1
42+ for (i in 1 .. < n) {
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+ class Solution {
6+ fun minLength (s : String , numOps : Int ): Int {
7+ val l = s.length
8+ var lingyi = 0
9+ var yiling = 0
10+ val pq: MutableList <Int > = ArrayList <Int >()
11+ var thisone = s.get(0 )
12+ var chang = 1
13+ if (thisone == ' 0' ) {
14+ yiling++
15+ } else {
16+ lingyi++
17+ }
18+ for (i in 1 .. < l) {
19+ val cur = s.get(i)
20+ if (cur == thisone) {
21+ chang++
22+ } else {
23+ if (chang >= 2 ) {
24+ pq.add(chang)
25+ }
26+ chang = 1
27+ thisone = cur
28+ }
29+ if (i % 2 == 0 ) {
30+ if (cur == ' 0' ) {
31+ yiling++
32+ } else {
33+ lingyi++
34+ }
35+ } else {
36+ if (cur == ' 0' ) {
37+ lingyi++
38+ } else {
39+ yiling++
40+ }
41+ }
42+ }
43+ if (numOps >= lingyi || numOps >= yiling) {
44+ return 1
45+ }
46+ if (chang >= 2 ) {
47+ pq.add(chang)
48+ }
49+ var one = - 1
50+ var two = - 1
51+ for (cur in pq) {
52+ if (cur > one) {
53+ two = one
54+ one = cur
55+ } else if (cur > two) {
56+ two = cur
57+ }
58+ }
59+ if (two == - 1 ) {
60+ return if (one / (numOps + 1 ) > 1 ) one / (numOps + 1 ) else 2
61+ }
62+ if (numOps == 0 ) {
63+ return one
64+ }
65+ if (numOps == 1 ) {
66+ return if (one / 2 > two) (if (one / 2 == 1 ) 2 else one / 2 ) else two
67+ }
68+ var left = 2
69+ var right = l / (numOps + 1 )
70+ while (left < right) {
71+ val mid = left + (right - left) / 2
72+ var sum = 0
73+ for (integer in pq) {
74+ sum + = integer / (mid + 1 )
75+ }
76+ if (sum <= numOps) {
77+ right = mid
78+ } else {
79+ left = mid + 1
80+ }
81+ }
82+ return left
83+ }
84+ }
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 org.hamcrest.CoreMatchers.equalTo
4+ import org.hamcrest.MatcherAssert.assertThat
5+ import org.junit.jupiter.api.Test
6+
7+ internal class SolutionTest {
8+ @Test
9+ fun minLength () {
10+ assertThat<Int >(Solution ().minLength(" 000001" , 1 ), equalTo<Int >(2 ))
11+ }
12+
13+ @Test
14+ fun minLength2 () {
15+ assertThat<Int >(Solution ().minLength(" 0000" , 2 ), equalTo<Int >(1 ))
16+ }
17+
18+ @Test
19+ fun minLength3 () {
20+ assertThat<Int >(Solution ().minLength(" 0101" , 0 ), equalTo<Int >(1 ))
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package g3301_3400.s3399_smallest_substring_with_identical_characters_ii
2+
3+ import org.hamcrest.CoreMatchers.equalTo
4+ import org.hamcrest.MatcherAssert.assertThat
5+ import org.junit.jupiter.api.Test
6+
7+ internal class SolutionTest {
8+ @Test
9+ fun minLength () {
10+ assertThat<Int >(Solution ().minLength(" 000001" , 1 ), equalTo<Int >(2 ))
11+ }
12+
13+ @Test
14+ fun minLength2 () {
15+ assertThat<Int >(Solution ().minLength(" 0000" , 2 ), equalTo<Int >(1 ))
16+ }
17+
18+ @Test
19+ fun minLength3 () {
20+ assertThat<Int >(Solution ().minLength(" 0101" , 0 ), equalTo<Int >(1 ))
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments