Problem Statement
Given a string s consisting of lowercase English letters, repeatedly remove adjacent duplicate characters until no such duplicates remain. Return the final string.
Pattern Used
Stack (String used as Stack)
Approach (High-level)
Time & Space Complexity
O(n)O(n)LeetCode
class Solution {
public:
string removeDuplicates(string s) {
//intialise ans string as empty string
string ans = "";
int n = s.length();
for(int i=0; i<n; i++) {
char currCharacter = s[i];
if(ans.empty() || currCharacter != ans.back()) {
//if ans is empty, seedha push karo
ans.push_back(currCharacter);
}//rightmost character of ans = ans.back()
else if(currCharacter == ans.back()) {
ans.pop_back();
}
}
return ans;
}
};
Problem Statement
Given two strings s and part, repeatedly remove the leftmost occurrence of the substring part from s until no such occurrence exists. Return the final string.