Skip to content

Commit d8e9106

Browse files
committed
1576. Replace All ?'s to Avoid Consecutive Repeating Characters: AC
1 parent c20fcda commit d8e9106

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1188,3 +1188,4 @@ mod s1572_matrix_diagonal_sum;
11881188
mod s1573_number_of_ways_to_split_a_string;
11891189
mod s1574_shortest_subarray_to_be_removed_to_make_array_sorted;
11901190
mod s1575_count_all_possible_routes;
1191+
mod s1576_replace_all_s_to_avoid_consecutive_repeating_characters;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* [1576] Replace All ?'s to Avoid Consecutive Repeating Characters
3+
*
4+
* Given a string s containing only lowercase English letters and the '?' character, convert all the '?' characters into lowercase letters such that the final string does not contain any consecutive repeating characters. You cannot modify the non '?' characters.
5+
* It is guaranteed that there are no consecutive repeating characters in the given string except for '?'.
6+
* Return the final string after all the conversions (possibly zero) have been made. If there is more than one solution, return any of them. It can be shown that an answer is always possible with the given constraints.
7+
*
8+
* Example 1:
9+
*
10+
* Input: s = "?zs"
11+
* Output: "azs"
12+
* Explanation: There are 25 solutions for this problem. From "azs" to "yzs", all are valid. Only "z" is an invalid modification as the string will consist of consecutive repeating characters in "zzs".
13+
*
14+
* Example 2:
15+
*
16+
* Input: s = "ubv?w"
17+
* Output: "ubvaw"
18+
* Explanation: There are 24 solutions for this problem. Only "v" and "w" are invalid modifications as the strings will consist of consecutive repeating characters in "ubvvw" and "ubvww".
19+
*
20+
*
21+
* Constraints:
22+
*
23+
* 1 <= s.length <= 100
24+
* s consist of lowercase English letters and '?'.
25+
*
26+
*/
27+
pub struct Solution {}
28+
29+
// problem: https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/
30+
// discuss: https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/discuss/?currentPage=1&orderBy=most_votes&query=
31+
32+
// submission codes start here
33+
34+
impl Solution {
35+
pub fn modify_string(s: String) -> String {
36+
let mut result = String::with_capacity(s.len());
37+
let mut peekable = s.chars().peekable();
38+
39+
while let Some(curr) = peekable.next() {
40+
if curr != '?' {
41+
result.push(curr);
42+
continue;
43+
}
44+
45+
result.push(
46+
match (
47+
result.chars().last().unwrap_or('c'),
48+
*peekable.peek().unwrap_or(&'c'),
49+
) {
50+
('a', 'a') => 'b',
51+
('b', 'b') => 'a',
52+
('a', 'b') | ('b', 'a') => 'c',
53+
('a', _) | (_, 'a') => 'b',
54+
('b', _) | (_, 'b') => 'a',
55+
(_, _) => 'a',
56+
},
57+
)
58+
}
59+
60+
result
61+
}
62+
}
63+
64+
// submission codes end
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use super::*;
69+
70+
#[test]
71+
fn test_1576_example_1() {}
72+
}

0 commit comments

Comments
 (0)