Skip to content

Commit f5b6956

Browse files
committed
1910. Remove All Occurrences of a Substring: AC
1 parent 73a74a6 commit f5b6956

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
@@ -1439,3 +1439,4 @@ mod s1904_the_number_of_full_rounds_you_have_played;
14391439
mod s1905_count_sub_islands;
14401440
mod s1906_minimum_absolute_difference_queries;
14411441
mod s1909_remove_one_element_to_make_the_array_strictly_increasing;
1442+
mod s1910_remove_all_occurrences_of_a_substring;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* [1910] Remove All Occurrences of a Substring
3+
*
4+
* Given two strings s and part, perform the following operation on s until all occurrences of the substring part are removed:
5+
*
6+
* Find the leftmost occurrence of the substring part and remove it from s.
7+
*
8+
* Return s after removing all occurrences of part.
9+
* A substring is a contiguous sequence of characters in a string.
10+
*
11+
* Example 1:
12+
*
13+
* Input: s = "daabcbaabcbc", part = "abc"
14+
* Output: "dab"
15+
* Explanation: The following operations are done:
16+
* - s = "da<u>abc</u>baabcbc", remove "abc" starting at index 2, so s = "dabaabcbc".
17+
* - s = "daba<u>abc</u>bc", remove "abc" starting at index 4, so s = "dababc".
18+
* - s = "dab<u>abc</u>", remove "abc" starting at index 3, so s = "dab".
19+
* Now s has no occurrences of "abc".
20+
*
21+
* Example 2:
22+
*
23+
* Input: s = "axxxxyyyyb", part = "xy"
24+
* Output: "ab"
25+
* Explanation: The following operations are done:
26+
* - s = "axxx<u>xy</u>yyyb", remove "xy" starting at index 4 so s = "axxxyyyb".
27+
* - s = "axx<u>xy</u>yyb", remove "xy" starting at index 3 so s = "axxyyb".
28+
* - s = "ax<u>xy</u>yb", remove "xy" starting at index 2 so s = "axyb".
29+
* - s = "a<u>xy</u>b", remove "xy" starting at index 1 so s = "ab".
30+
* Now s has no occurrences of "xy".
31+
*
32+
*
33+
* Constraints:
34+
*
35+
* 1 <= s.length <= 1000
36+
* 1 <= part.length <= 1000
37+
* s​​​​​​ and part consists of lowercase English letters.
38+
*
39+
*/
40+
pub struct Solution {}
41+
42+
// problem: https://leetcode.com/problems/remove-all-occurrences-of-a-substring/
43+
// discuss: https://leetcode.com/problems/remove-all-occurrences-of-a-substring/discuss/?currentPage=1&orderBy=most_votes&query=
44+
45+
// submission codes start here
46+
47+
impl Solution {
48+
pub fn remove_occurrences(s: String, part: String) -> String {
49+
s.chars()
50+
.fold(vec![], |mut stack, c| {
51+
stack.push(c);
52+
if stack.len() < part.len() {
53+
return stack;
54+
}
55+
if part
56+
.chars()
57+
.zip(stack.iter().skip(stack.len() - part.len()))
58+
.all(|(i1, i2)| i1 == *i2)
59+
{
60+
stack = stack
61+
.iter()
62+
.take(stack.len() - part.len())
63+
.map(|c| *c)
64+
.collect::<Vec<_>>();
65+
}
66+
stack
67+
})
68+
.into_iter()
69+
.collect::<_>()
70+
}
71+
}
72+
73+
// submission codes end
74+
75+
#[cfg(test)]
76+
mod tests {
77+
use super::*;
78+
79+
#[test]
80+
fn test_1910_example_1() {
81+
let s = "daabcbaabcbc".to_string();
82+
let part = "abc".to_string();
83+
84+
let result = "dab".to_string();
85+
86+
assert_eq!(Solution::remove_occurrences(s, part), result);
87+
}
88+
89+
#[test]
90+
fn test_1910_example_2() {
91+
let s = "axxxxyyyyb".to_string();
92+
let part = "xy".to_string();
93+
94+
let result = "ab".to_string();
95+
96+
assert_eq!(Solution::remove_occurrences(s, part), result);
97+
}
98+
}

0 commit comments

Comments
 (0)