Skip to content

Commit 30f476e

Browse files
committed
LC 2914. Minimum Number of Changes to Make Binary String Beautiful (Rust)
1 parent 1d9c3b1 commit 30f476e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ to the solution in this repository.
770770
| [2849. Determine if a Cell Is Reachable at a Given Time][lc2849] | 🟠 Medium | [![rust](res/rs.png)][lc2849rs] |
771771
| [2864. Maximum Odd Binary Number][lc2864] | 🟢 Easy | [![rust](res/rs.png)][lc2864rs] |
772772
| [2870. Minimum Number of Operations to Make Array Empty][lc2870] | 🟠 Medium | [![rust](res/rs.png)][lc2870rs] |
773+
| [2914. Minimum Number of Changes to Make Binary String Beautiful][lc2914] | 🟠 Medium | [![rust](res/rs.png)][lc2914rs] |
773774
| [2938. Separate Black and White Balls][lc2938] | 🟠 Medium | [![rust](res/rs.png)][lc2938rs] |
774775
| [2958. Length of Longest Subarray With at Most K Frequency][lc2958] | 🟠 Medium | [![rust](res/rs.png)][lc2958rs] |
775776
| [2962. Count Subarrays Where Max Element Appears at Least K Times][lc2962] | 🟠 Medium | [![rust](res/rs.png)][lc2962rs] |
@@ -2474,6 +2475,8 @@ to the solution in this repository.
24742475
[lc2864rs]: leetcode/maximum-odd-binary-number.rs
24752476
[lc2870]: https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/
24762477
[lc2870rs]: leetcode/minimum-number-of-operations-to-make-array-empty.rs
2478+
[lc2914]: https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/
2479+
[lc2914rs]: leetcode/minimum-number-of-changes-to-make-binary-string-beautiful.rs
24772480
[lc2938]: https://leetcode.com/problems/separate-black-and-white-balls/
24782481
[lc2938rs]: leetcode/separate-black-and-white-balls.rs
24792482
[lc2958]: https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// 2914. Minimum Number of Changes to Make Binary String Beautiful
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/
5+
//
6+
// Tags: String
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Iterate over pairs of values, if they don't match, we need to do 1 more change.
11+
///
12+
/// Time complexity: O(n) - We iterate over pairs of values in the input checking if they match
13+
/// in constant time.
14+
/// Space complexity: O(n) - The byte slice gets allocated, not sure how to avoid that.
15+
///
16+
/// Runtime 1 ms Beats 100%
17+
/// Memory 2.27 MB Beats 83%
18+
pub fn min_changes(s: String) -> i32 {
19+
s.as_bytes()
20+
.chunks(2)
21+
.map(|chunk| if chunk[0] == chunk[1] { 0 } else { 1 })
22+
.sum()
23+
}
24+
}
25+
26+
// Tests.
27+
fn main() {
28+
let tests = [("1001", 2), ("10", 1), ("0000", 0), ("11000111", 1)];
29+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
30+
let mut success = 0;
31+
for (i, t) in tests.iter().enumerate() {
32+
let res = Solution::min_changes(t.0.to_owned());
33+
if res == t.1 {
34+
success += 1;
35+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
36+
} else {
37+
println!(
38+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
39+
i, t.1, res
40+
);
41+
}
42+
}
43+
println!();
44+
if success == tests.len() {
45+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
46+
} else if success == 0 {
47+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
48+
} else {
49+
println!(
50+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
51+
tests.len() - success
52+
)
53+
}
54+
}

0 commit comments

Comments
 (0)