Given two strings s and part, perform the following operation on s until all occurrences of the substring part are removed:
- Find the leftmost occurrence of the substring
partand remove it froms.
Return s after removing all occurrences of part.
A substring is a contiguous sequence of characters in a string.
Constraints:
1 <= s.length <= 10001 <= part.length <= 1000sandpartconsists of lowercase English letters.
Stack
func removeOccurrences(s string, part string) string {
stk := []byte{}
strLenght := len(s)
partLength := len(part)
for index := 0; index < strLenght; index++ {
stk = append(stk, s[index])
if (len(stk) >= partLength && checkMatch(stk, part, partLength)){
for j := 0; j < partLength; j++ {
stk = stk[:len(stk)-1]
}
}
}
result := ""
for (len(stk)>0){
result = string(stk[len(stk)-1]) + result
stk = stk[:len(stk)-1]
}
return result
}
func checkMatch( stk []byte, part string, partLength int ) bool{
temp := stk
for partIndex := partLength - 1; partIndex >= 0; partIndex-- {
if temp[len(temp)-1] != part[partIndex] {
return false
}
temp = temp[:len(temp)-1]
}
return true;
}Intuition
The goal is to remove all occurrences of the substring part from the string s. Instead of scanning and reconstructing the string multiple times, we simulate the process with a stack. As we iterate through s, we push each character onto the stack. After each push, we check if the top of the stack ends with part. If it does, we remove (pop) those characters immediately. This way, the removal is done incrementally and efficiently.
Algorithm
-
Initialization:
- Create an empty stack (implemented as a slice of bytes) called
stk. - Determine the lengths of
sandpart(stored instrLenghtandpartLengthrespectively).
- Create an empty stack (implemented as a slice of bytes) called
-
Processing Characters:
- Loop through each character in
s. - Append the current character to
stk. - If the size of
stkis at least as long aspartLength, call the helper functioncheckMatchto see if the last characters of the stack matchpart. - If a match is found, remove the last
partLengthcharacters fromstk.
- Loop through each character in
-
Constructing the Result:
- After processing all characters, rebuild the final string by popping all characters from
stkand concatenating them in reverse order (since the stack's order is from bottom to top).
- After processing all characters, rebuild the final string by popping all characters from
-
Helper Function - checkMatch:
- This function checks if the top of the stack matches
partby comparing the lastpartLengthcharacters in the stack (in reverse order) withpart. - It returns
trueif they match; otherwise,false.
- This function checks if the top of the stack matches
Complexity Analysis
-
Time Complexity:
In the worst-case scenario, for each of the n characters ins, we may perform a check that takes up to m comparisons (where m is the length ofpart). Therefore, the worst-case time complexity isO(n ⋅ m). -
Space Complexity:
The stackstkcan hold up to n characters in the worst case. Additionally, the helper function uses a temporary variable for comparisons. This gives an overall space complexity ofO(n + m).
Wrap up
If you found this guide helpful, consider subscribing to my newsletter on jyotirmoy.dev/blogs , You can also follow me on Twitter jyotirmoydotdev for updates and more content.

