Skip to content

Commit 6a6e861

Browse files
committed
LC 1957. Delete Characters to Make Fancy String (Rust)
1 parent e2e5c09 commit 6a6e861

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ to the solution in this repository.
640640
| [1937. Maximum Number of Points with Cost][lc1937] | 🟠 Medium | [![rust](res/rs.png)][lc1937rs] |
641641
| [1942. The Number of the Smallest Unoccupied Chair][lc1942] | 🟠 Medium | [![rust](res/rs.png)][lc1942rs] |
642642
| [1945. Sum of Digits of String After Convert][lc1945] | 🟢 Easy | [![rust](res/rs.png)][lc1945rs] |
643+
| [1957. Delete Characters to Make Fancy String][lc1957] | 🟢 Easy | [![rust](res/rs.png)][lc1957rs] |
643644
| [1962. Remove Stones to Minimize the Total][lc1962] | 🟠 Medium | [![python](res/py.png)][lc1962py] |
644645
| [1963. Minimum Number of Swaps to Make the String Balanced][lc1963] | 🟠 Medium | [![rust](res/rs.png)][lc1963rs] |
645646
| [1964. Find the Longest Valid Obstacle Course at Each Position][lc1964] | 🔴 Hard | [![python](res/py.png)][lc1964py] [![rust](res/rs.png)][lc1964rs] |
@@ -2190,6 +2191,8 @@ to the solution in this repository.
21902191
[lc1942rs]: leetcode/the-number-of-the-smallest-unoccupied-chair.rs
21912192
[lc1945]: https://leetcode.com/problems/sum-of-digits-of-string-after-convert/
21922193
[lc1945rs]: leetcode/sum-of-digits-of-string-after-convert.rs
2194+
[lc1957]: https://leetcode.com/problems/delete-characters-to-make-fancy-string/
2195+
[lc1957rs]: leetcode/delete-characters-to-make-fancy-string.rs
21932196
[lc1962]: https://leetcode.com/problems/remove-stones-to-minimize-the-total/
21942197
[lc1962py]: leetcode/remove-stones-to-minimize-the-total.py
21952198
[lc1963]: https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// 1957. Delete Characters to Make Fancy String
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/delete-characters-to-make-fancy-string/
5+
//
6+
// Tags: String
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Iterate over the input counting the frequency of characters, append to the result string
11+
/// while the current character is repeated a maximum of 2 times.
12+
///
13+
/// Time complexity: O(n)
14+
/// Space complexity: O(n)
15+
///
16+
/// Runtime 2 ms Beats 100%
17+
/// Memory 2.51 MB Beats 28.57%
18+
pub fn make_fancy_string(s: String) -> String {
19+
let mut res = String::with_capacity(s.len());
20+
let mut last = '?';
21+
let mut repeated = false;
22+
for c in s.chars() {
23+
if c == last {
24+
if repeated {
25+
continue;
26+
}
27+
repeated = true;
28+
} else {
29+
last = c;
30+
repeated = false;
31+
}
32+
res.push(c);
33+
}
34+
res
35+
}
36+
}
37+
38+
// Tests.
39+
fn main() {
40+
let tests = [("leeetcode", "leetcode"), ("aaabaaaa", "aabaa")];
41+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
42+
let mut success = 0;
43+
for (i, t) in tests.iter().enumerate() {
44+
let res = Solution::make_fancy_string(t.0.to_string());
45+
if res == t.1 {
46+
success += 1;
47+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
48+
} else {
49+
println!(
50+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
51+
i, t.1, res
52+
);
53+
}
54+
}
55+
println!();
56+
if success == tests.len() {
57+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
58+
} else if success == 0 {
59+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
60+
} else {
61+
println!(
62+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
63+
tests.len() - success
64+
)
65+
}
66+
}

0 commit comments

Comments
 (0)