Two Pointers
class Solution:
def removeDuplicates(self, s: str) -> str:
def solu1(s):
""" tc O(N) sc O(N)
main idea: stack
"""
st = []
for c in s:
if st and st[-1] == c :
st.pop()
else:
st.append(c)
return ''.join(st)
Stack
def solu2(s):
""" tc O(N) sc O(1)
@i: hold index set for next char in output
@cur: index of current iteration in the input string
"""
s = list(s)
i,n = 0, len(s)
for cur in range(n):
s[i] = s[cur]
if i > 0 and s[i-1] == s[i]: #duplicates = 2,can extend to k
i -= 2
i += 1
return ''.join(s[:i])