Skip to content

Commit c1bb737

Browse files
2914: calculate the minimum number of changes to make a binary String beautiful
1 parent eaaede9 commit c1bb737

File tree

4 files changed

+95
-15
lines changed

4 files changed

+95
-15
lines changed

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ My solutions to LeetCode problems in Kotlin.
44

55
## Solutions
66

7-
| # | Title | Difficulty |
8-
|:------------------------------------------------------------------:|:------------------------------------------------------------------------------------------------------------:|:----------:|
9-
| [1](https://leetcode.com/problems/two-sum/description/) | [Two Sum](src/main/kotlin/com/schmoczer/leetcode/_0001/TwoSum.kt) | Easy |
10-
| [5](https://leetcode.com/problems/longest-palindromic-substring/) | [Longest Palindromic Substring](src/main/kotlin/com/schmoczer/leetcode/_0005/LongestPalindromicSubstring.kt) | Medium |
11-
| [8](https://leetcode.com/problems/string-to-integer-atoi/) | [String to Integer (atoi)](src/main/kotlin/com/schmoczer/leetcode/_0008/StringToInteger.kt) | Medium |
12-
| [20](https://leetcode.com/problems/valid-parentheses/) | [Valid Parentheses](src/main/kotlin/com/schmoczer/leetcode/_0020/ValidParentheses.kt) | Easy |
13-
| [49](https://leetcode.com/problems/group-anagrams/) | [Group Anagrams](src/main/kotlin/com/schmoczer/leetcode/_0049/GroupAnagrams.kt) | Medium |
14-
| [125](https://leetcode.com/problems/valid-palindrome/) | [Valid Palindrome](src/main/kotlin/com/schmoczer/leetcode/_0125/ValidPalindrome.kt) | Easy |
15-
| [151](https://leetcode.com/problems/reverse-words-in-a-string/) | [Reverse Words in a String](src/main/kotlin/com/schmoczer/leetcode/_0151/ReverseWordsInString.kt) | Medium |
16-
| [186](https://leetcode.com/problems/reverse-words-in-a-string-ii/) | [Reverse Words in a String II](src/main/kotlin/com/schmoczer/leetcode/_0186/ReverseWordsInStringInPlace.kt) | Medium |
17-
| [206](https://leetcode.com/problems/reverse-linked-list/) | [Reverse Linked List](src/main/kotlin/com/schmoczer/leetcode/_0206/ReverseLinkedList.kt) | Easy |
18-
| [344](https://leetcode.com/problems/reverse-string/) | [Reverse String](src/main/kotlin/com/schmoczer/leetcode/_0344/ReverseString.kt) | Easy |
19-
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
20-
| [2490](https://leetcode.com/problems/circular-sentence/) | [Circular Sentence](src/main/kotlin/com/schmoczer/leetcode/_2490/CircularSentence.kt) | Easy |
21-
| [3163](https://leetcode.com/problems/string-compression-iii/) | [String Compression III](src/main/kotlin/com/schmoczer/leetcode/_3163/StringCompression3.kt) | Medium |
7+
| # | Title | Difficulty |
8+
|:------------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------------------:|:----------:|
9+
| [1](https://leetcode.com/problems/two-sum/description/) | [Two Sum](src/main/kotlin/com/schmoczer/leetcode/_0001/TwoSum.kt) | Easy |
10+
| [5](https://leetcode.com/problems/longest-palindromic-substring/) | [Longest Palindromic Substring](src/main/kotlin/com/schmoczer/leetcode/_0005/LongestPalindromicSubstring.kt) | Medium |
11+
| [8](https://leetcode.com/problems/string-to-integer-atoi/) | [String to Integer (atoi)](src/main/kotlin/com/schmoczer/leetcode/_0008/StringToInteger.kt) | Medium |
12+
| [20](https://leetcode.com/problems/valid-parentheses/) | [Valid Parentheses](src/main/kotlin/com/schmoczer/leetcode/_0020/ValidParentheses.kt) | Easy |
13+
| [49](https://leetcode.com/problems/group-anagrams/) | [Group Anagrams](src/main/kotlin/com/schmoczer/leetcode/_0049/GroupAnagrams.kt) | Medium |
14+
| [125](https://leetcode.com/problems/valid-palindrome/) | [Valid Palindrome](src/main/kotlin/com/schmoczer/leetcode/_0125/ValidPalindrome.kt) | Easy |
15+
| [151](https://leetcode.com/problems/reverse-words-in-a-string/) | [Reverse Words in a String](src/main/kotlin/com/schmoczer/leetcode/_0151/ReverseWordsInString.kt) | Medium |
16+
| [186](https://leetcode.com/problems/reverse-words-in-a-string-ii/) | [Reverse Words in a String II](src/main/kotlin/com/schmoczer/leetcode/_0186/ReverseWordsInStringInPlace.kt) | Medium |
17+
| [206](https://leetcode.com/problems/reverse-linked-list/) | [Reverse Linked List](src/main/kotlin/com/schmoczer/leetcode/_0206/ReverseLinkedList.kt) | Easy |
18+
| [344](https://leetcode.com/problems/reverse-string/) | [Reverse String](src/main/kotlin/com/schmoczer/leetcode/_0344/ReverseString.kt) | Easy |
19+
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
20+
| [2490](https://leetcode.com/problems/circular-sentence/) | [Circular Sentence](src/main/kotlin/com/schmoczer/leetcode/_2490/CircularSentence.kt) | Easy |
21+
| [2914](https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/) | [Minimum Number of Changes to Make Binary String Beautiful](src/main/kotlin/com/schmoczer/leetcode/_2914/MinChanges.kt) | Medium |
22+
| [3163](https://leetcode.com/problems/string-compression-iii/) | [String Compression III](src/main/kotlin/com/schmoczer/leetcode/_3163/StringCompression3.kt) | Medium |
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.schmoczer.leetcode._2914
2+
3+
class MinChanges {
4+
fun minChanges(s: String): Int {
5+
var changes = 0
6+
for (i in 2..s.length step 2) {
7+
val part = s.substring(i - 2, i)
8+
if (part == "01" || part == "10") {
9+
changes++
10+
}
11+
}
12+
return changes
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Minimum Number of Changes to Make Binary String Beautiful
2+
3+
You are given a 0-indexed binary string `s` having an even length.
4+
5+
A string is beautiful if it's possible to partition it into one or more substrings such that:
6+
7+
- Each substring has an even length.
8+
- Each substring contains only `1`'s or only `0`'s.
9+
10+
You can change any character in `s` to `0` or `1`.
11+
12+
Return the minimum number of changes required to make the string `s` beautiful.
13+
14+
Example 1:
15+
16+
> Input: s = "1001"
17+
>
18+
> Output: 2
19+
20+
Example 2:
21+
22+
> Input: s = "10"
23+
>
24+
> Output: 1
25+
26+
Example 3:
27+
28+
> Input: s = "0000"
29+
>
30+
> Output: 0
31+
32+
Constraints:
33+
34+
- `2 <= s.length <= 105`
35+
- `s` has an even length.
36+
- `s[i]` is either `'0'` or `'1'`.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.schmoczer.leetcode._2914
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
class MinChangesTest {
8+
private lateinit var sut: MinChanges
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = MinChanges()
13+
}
14+
15+
@Test
16+
fun `1001 needs 2 changes`() {
17+
assertEquals(2, sut.minChanges("1001"))
18+
}
19+
20+
@Test
21+
fun `10 nees 1 change`() {
22+
assertEquals(1, sut.minChanges("10"))
23+
}
24+
25+
@Test
26+
fun `0000 needs 0 changes`() {
27+
assertEquals(0, sut.minChanges("0000"))
28+
}
29+
}

0 commit comments

Comments
 (0)