Class Solution
-
- All Implemented Interfaces:
public final class Solution1910 - Remove All Occurrences of a Substring.
Medium
Given two strings
sandpart, perform the following operation onsuntil all occurrences of the substringpartare removed:Find the leftmost occurrence of the substring
partand remove it froms.
Return
safter removing all occurrences ofpart.A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = "daabcbaabcbc", part = "abc"
Output: "dab"
Explanation: The following operations are done:
s = "daabcbaabcbc", remove "abc" starting at index 2, so s = "dabaabcbc".
s = "dabaabcbc", remove "abc" starting at index 4, so s = "dababc".
s = "dababc", remove "abc" starting at index 3, so s = "dab". Now s has no occurrences of "abc".
Example 2:
Input: s = "axxxxyyyyb", part = "xy"
Output: "ab"
Explanation: The following operations are done:
s = "axxxxyyyyb", remove "xy" starting at index 4 so s = "axxxyyyb".
s = "axxxyyyb", remove "xy" starting at index 3 so s = "axxyyb".
s = "axxyyb", remove "xy" starting at index 2 so s = "axyb".
s = "axyb", remove "xy" starting at index 1 so s = "ab". Now s has no occurrences of "xy".
Constraints:
1 <= s.length <= 10001 <= part.length <= 1000sandpartconsists of lowercase English letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final StringremoveOccurrences(String s, String part)-
-
Method Detail
-
removeOccurrences
final String removeOccurrences(String s, String part)
-
-
-
-