diff options
Diffstat (limited to 'remove_substring.cpp')
-rw-r--r-- | remove_substring.cpp | 29 |
1 files changed, 3 insertions, 26 deletions
diff --git a/remove_substring.cpp b/remove_substring.cpp index f3744ab..a5572be 100644 --- a/remove_substring.cpp +++ b/remove_substring.cpp @@ -10,34 +10,11 @@ */ class Solution { - bool check(stack<char> stk, string part) { - int n = part.size(), j = n - 1; - while (j >= 0 && stk.top() == part[j]) - stk.pop(), j--; - return j == -1; - } - public: string removeOccurrences(string s, string part) { - int n1 = s.size(), n2 = part.size(); - stack<char> stk; - string str = ""; - for (int i = 0; i < n1; i++) { - stk.push(s[i]); - if (stk.size() >= n2) - if (check(stk, part)) { - int cnt = n2; - while (cnt--) - stk.pop(); - } - } - string ans = ""; - while (!stk.empty()) { - ans += stk.top(); - stk.pop(); - } - reverse(ans.begin(), ans.end()); - return ans; + while (s.size() && s.find(part) < s.size()) + s.erase(s.find(part), part.size()); + return s; } }; |