Skip to content

Commit 7249325

Browse files
committed
1957. Delete Characters to Make Fancy String: AC
1 parent 1216285 commit 7249325

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1474,3 +1474,4 @@ mod s1952_three_divisors;
14741474
mod s1953_maximum_number_of_weeks_for_which_you_can_work;
14751475
mod s1954_minimum_garden_perimeter_to_collect_enough_apples;
14761476
mod s1955_count_number_of_special_subsequences;
1477+
mod s1957_delete_characters_to_make_fancy_string;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* [1957] Delete Characters to Make Fancy String
3+
*
4+
* A fancy string is a string where no three consecutive characters are equal.
5+
* Given a string s, delete the minimum possible number of characters from s to make it fancy.
6+
* Return the final string after the deletion. It can be shown that the answer will always be unique.
7+
*
8+
* Example 1:
9+
*
10+
* Input: s = "le<u>e</u>etcode"
11+
* Output: "leetcode"
12+
* Explanation:
13+
* Remove an 'e' from the first group of 'e's to create "leetcode".
14+
* No three consecutive characters are equal, so return "leetcode".
15+
*
16+
* Example 2:
17+
*
18+
* Input: s = "<u>a</u>aab<u>aa</u>aa"
19+
* Output: "aabaa"
20+
* Explanation:
21+
* Remove an 'a' from the first group of 'a's to create "aabaaaa".
22+
* Remove two 'a's from the second group of 'a's to create "aabaa".
23+
* No three consecutive characters are equal, so return "aabaa".
24+
*
25+
* Example 3:
26+
*
27+
* Input: s = "aab"
28+
* Output: "aab"
29+
* Explanation: No three consecutive characters are equal, so return "aab".
30+
*
31+
*
32+
* Constraints:
33+
*
34+
* 1 <= s.length <= 10^5
35+
* s consists only of lowercase English letters.
36+
*
37+
*/
38+
pub struct Solution {}
39+
40+
// problem: https://leetcode.com/problems/delete-characters-to-make-fancy-string/
41+
// discuss: https://leetcode.com/problems/delete-characters-to-make-fancy-string/discuss/?currentPage=1&orderBy=most_votes&query=
42+
43+
// submission codes start here
44+
45+
impl Solution {
46+
pub fn make_fancy_string(s: String) -> String {
47+
s.chars()
48+
.fold(
49+
("".to_string(), 0, ' '),
50+
|(mut result, counter, prev_ch), ch| {
51+
if ch == prev_ch {
52+
if counter < 1 {
53+
result.push_str(ch.to_string().as_str());
54+
}
55+
(result, counter + 1, ch)
56+
} else {
57+
result.push_str(ch.to_string().as_str());
58+
(result, 0, ch)
59+
}
60+
},
61+
)
62+
.0
63+
}
64+
}
65+
66+
// submission codes end
67+
68+
#[cfg(test)]
69+
mod tests {
70+
use super::*;
71+
72+
#[test]
73+
fn test_1957_example_1() {
74+
let s = "leeetcode".to_string();
75+
76+
let result = "leetcode".to_string();
77+
78+
assert_eq!(Solution::make_fancy_string(s), result);
79+
}
80+
81+
#[test]
82+
fn test_1957_example_2() {
83+
let s = "aaabaaaa".to_string();
84+
85+
let result = "aabaa".to_string();
86+
87+
assert_eq!(Solution::make_fancy_string(s), result);
88+
}
89+
90+
#[test]
91+
fn test_1957_example_3() {
92+
let s = "aab".to_string();
93+
94+
let result = "aab".to_string();
95+
96+
assert_eq!(Solution::make_fancy_string(s), result);
97+
}
98+
}

0 commit comments

Comments
 (0)