-
Notifications
You must be signed in to change notification settings - Fork 43
/
remove-all-adjacent-duplicates-in-string.py
130 lines (107 loc) · 3.65 KB
/
remove-all-adjacent-duplicates-in-string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
1047. Remove All Adjacent Duplicates In String
Easy
2997
You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Example 1:
Input: s = "abbaca"
Output: "ca"
Explanation:
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
Example 2:
Input: s = "azxxzy"
Output: "ay"
Constraints:
1 <= s.length <= 105
s consists of lowercase English letters.
"""
# V0
# IDEA : STACK
class Solution:
def removeDuplicates(self, x):
# edge
if not x:
return
stack = []
"""
NOTE !!! below op
"""
for i in range(len(x)):
# NOTE !!! : trick here : if stack last element == current x's element
# -> we pop last stack element
# -> and NOT add current element
if stack and stack[-1] == x[i]:
stack.pop(-1)
# if stack last element != current x's element
# -> we append x[i]
else:
stack.append(x[i])
return "".join(stack)
# V0'
# IDEA : TWO POINTERS
# -> pointers : end, c
class Solution:
def removeDuplicates(self, S):
end = -1
a = list(S)
for c in a:
if end >= 0 and a[end] == c:
end -= 1
else:
end += 1
a[end] = c
return ''.join(a[: end + 1])
# V1
# IDEA : STACK
# https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/294893/JavaC%2B%2BPython-Two-Pointers-and-Stack-Solution
class Solution:
def removeDuplicates(self, S):
res = []
for c in S:
if res and res[-1] == c:
res.pop()
else:
res.append(c)
return "".join(res)
# V1'
# IDEA : TWO POINTERS
# https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/294964/JavaPython-3-three-easy-iterative-codes-w-brief-explanation-analysis-and-follow-up.
class Solution:
def removeDuplicates(self, S: str) -> str:
end, a = -1, list(S)
for c in a:
if end >= 0 and a[end] == c:
end -= 1
else:
end += 1
a[end] = c
return ''.join(a[: end + 1])
# V1''
# IDEA : REPLACE
# https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/solution/
from string import ascii_lowercase
class Solution:
def removeDuplicates(self, S: str) -> str:
# generate 26 possible duplicates
duplicates = [2 * ch for ch in ascii_lowercase]
prev_length = -1
while prev_length != len(S):
prev_length = len(S)
for d in duplicates:
S = S.replace(d, '')
return S
# V1'''
# IDEA : STACK
# https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/solution/
class Solution:
def removeDuplicates(self, S: str) -> str:
output = []
for ch in S:
if output and ch == output[-1]:
output.pop()
else:
output.append(ch)
return ''.join(output)
# V2