Skip to content

Commit c220a93

Browse files
committed
feat: add solutions to lc problem: No.1047
No.1047.Remove All Adjacent Duplicates In String
1 parent 57ae56f commit c220a93

File tree

7 files changed

+190
-50
lines changed

7 files changed

+190
-50
lines changed

solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README.md

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@
3535

3636
<!-- 这里可写通用的实现逻辑 -->
3737

38-
栈实现。
38+
**方法一:栈**
3939

40-
遍历字符串 S 中的每个字符 s,若栈为空或者栈顶值不等于字符 s,s 入栈,否则栈顶元素出栈。
40+
遍历字符串 `s` 中的每个字符 `c`,若栈为空或者栈顶值不等于字符 `c`,将 `c` 入栈,否则栈顶元素出栈。
4141

4242
最后返回栈中元素所组成的字符串。
4343

44+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 `s` 的长度。
45+
4446
<!-- tabs:start -->
4547

4648
### **Python3**
@@ -49,14 +51,14 @@
4951

5052
```python
5153
class Solution:
52-
def removeDuplicates(self, S: str) -> str:
53-
res = []
54-
for s in S:
55-
if not res or res[-1] != s:
56-
res.append(s)
54+
def removeDuplicates(self, s: str) -> str:
55+
stk = []
56+
for c in s:
57+
if stk and stk[-1] == c:
58+
stk.pop()
5759
else:
58-
res.pop()
59-
return ''.join(res)
60+
stk.append(c)
61+
return ''.join(stk)
6062
```
6163

6264
### **Java**
@@ -65,24 +67,75 @@ class Solution:
6567

6668
```java
6769
class Solution {
68-
public String removeDuplicates(String S) {
70+
public String removeDuplicates(String s) {
6971
StringBuilder sb = new StringBuilder();
70-
int top = -1;
71-
for (int i = 0, n = S.length(); i < n; ++i) {
72-
char s = S.charAt(i);
73-
if (top == -1 || sb.charAt(top) != s) {
74-
sb.append(s);
75-
++top;
72+
for (char c : s.toCharArray()) {
73+
if (sb.length() > 0 && sb.charAt(sb.length() - 1) == c) {
74+
sb.deleteCharAt(sb.length() - 1);
7675
} else {
77-
sb.deleteCharAt(top);
78-
--top;
76+
sb.append(c);
7977
}
8078
}
8179
return sb.toString();
8280
}
8381
}
8482
```
8583

84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
string removeDuplicates(string s) {
90+
string stk;
91+
for (char c : s) {
92+
if (!stk.empty() && stk[stk.size() - 1] == c) {
93+
stk.pop_back();
94+
} else {
95+
stk += c;
96+
}
97+
}
98+
return stk;
99+
}
100+
};
101+
```
102+
103+
### **Go**
104+
105+
```go
106+
func removeDuplicates(s string) string {
107+
stk := []rune{}
108+
for _, c := range s {
109+
if len(stk) > 0 && stk[len(stk)-1] == c {
110+
stk = stk[:len(stk)-1]
111+
} else {
112+
stk = append(stk, c)
113+
}
114+
}
115+
return string(stk)
116+
}
117+
```
118+
119+
### **JavaScript**
120+
121+
```js
122+
/**
123+
* @param {string} s
124+
* @return {string}
125+
*/
126+
var removeDuplicates = function (s) {
127+
const stk = [];
128+
for (const c of s) {
129+
if (stk.length && stk[stk.length - 1] == c) {
130+
stk.pop();
131+
} else {
132+
stk.push(c);
133+
}
134+
}
135+
return stk.join('');
136+
};
137+
```
138+
86139
### **...**
87140

88141
```

solution/1000-1099/1047.Remove All Adjacent Duplicates In String/README_EN.md

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,38 +43,89 @@ For example, in &quot;abbaca&quot; we could remove &quot;bb&quot; since the lett
4343

4444
```python
4545
class Solution:
46-
def removeDuplicates(self, S: str) -> str:
47-
res = []
48-
for s in S:
49-
if not res or res[-1] != s:
50-
res.append(s)
46+
def removeDuplicates(self, s: str) -> str:
47+
stk = []
48+
for c in s:
49+
if stk and stk[-1] == c:
50+
stk.pop()
5151
else:
52-
res.pop()
53-
return ''.join(res)
52+
stk.append(c)
53+
return ''.join(stk)
5454
```
5555

5656
### **Java**
5757

5858
```java
5959
class Solution {
60-
public String removeDuplicates(String S) {
60+
public String removeDuplicates(String s) {
6161
StringBuilder sb = new StringBuilder();
62-
int top = -1;
63-
for (int i = 0, n = S.length(); i < n; ++i) {
64-
char s = S.charAt(i);
65-
if (top == -1 || sb.charAt(top) != s) {
66-
sb.append(s);
67-
++top;
62+
for (char c : s.toCharArray()) {
63+
if (sb.length() > 0 && sb.charAt(sb.length() - 1) == c) {
64+
sb.deleteCharAt(sb.length() - 1);
6865
} else {
69-
sb.deleteCharAt(top);
70-
--top;
66+
sb.append(c);
7167
}
7268
}
7369
return sb.toString();
7470
}
7571
}
7672
```
7773

74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
string removeDuplicates(string s) {
80+
string stk;
81+
for (char c : s) {
82+
if (!stk.empty() && stk[stk.size() - 1] == c) {
83+
stk.pop_back();
84+
} else {
85+
stk += c;
86+
}
87+
}
88+
return stk;
89+
}
90+
};
91+
```
92+
93+
### **Go**
94+
95+
```go
96+
func removeDuplicates(s string) string {
97+
stk := []rune{}
98+
for _, c := range s {
99+
if len(stk) > 0 && stk[len(stk)-1] == c {
100+
stk = stk[:len(stk)-1]
101+
} else {
102+
stk = append(stk, c)
103+
}
104+
}
105+
return string(stk)
106+
}
107+
```
108+
109+
### **JavaScript**
110+
111+
```js
112+
/**
113+
* @param {string} s
114+
* @return {string}
115+
*/
116+
var removeDuplicates = function (s) {
117+
const stk = [];
118+
for (const c of s) {
119+
if (stk.length && stk[stk.length - 1] == c) {
120+
stk.pop();
121+
} else {
122+
stk.push(c);
123+
}
124+
}
125+
return stk.join('');
126+
};
127+
```
128+
78129
### **...**
79130

80131
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
string removeDuplicates(string s) {
4+
string stk;
5+
for (char c : s) {
6+
if (!stk.empty() && stk[stk.size() - 1] == c) {
7+
stk.pop_back();
8+
} else {
9+
stk += c;
10+
}
11+
}
12+
return stk;
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func removeDuplicates(s string) string {
2+
stk := []rune{}
3+
for _, c := range s {
4+
if len(stk) > 0 && stk[len(stk)-1] == c {
5+
stk = stk[:len(stk)-1]
6+
} else {
7+
stk = append(stk, c)
8+
}
9+
}
10+
return string(stk)
11+
}

solution/1000-1099/1047.Remove All Adjacent Duplicates In String/Solution.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
class Solution {
2-
public String removeDuplicates(String S) {
2+
public String removeDuplicates(String s) {
33
StringBuilder sb = new StringBuilder();
4-
int top = -1;
5-
for (int i = 0, n = S.length(); i < n; ++i) {
6-
char s = S.charAt(i);
7-
if (top == -1 || sb.charAt(top) != s) {
8-
sb.append(s);
9-
++top;
4+
for (char c : s.toCharArray()) {
5+
if (sb.length() > 0 && sb.charAt(sb.length() - 1) == c) {
6+
sb.deleteCharAt(sb.length() - 1);
107
} else {
11-
sb.deleteCharAt(top);
12-
--top;
8+
sb.append(c);
139
}
1410
}
1511
return sb.toString();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
var removeDuplicates = function (s) {
6+
const stk = [];
7+
for (const c of s) {
8+
if (stk.length && stk[stk.length - 1] == c) {
9+
stk.pop();
10+
} else {
11+
stk.push(c);
12+
}
13+
}
14+
return stk.join('');
15+
};
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
2-
def removeDuplicates(self, S: str) -> str:
3-
res = []
4-
for s in S:
5-
if not res or res[-1] != s:
6-
res.append(s)
2+
def removeDuplicates(self, s: str) -> str:
3+
stk = []
4+
for c in s:
5+
if stk and stk[-1] == c:
6+
stk.pop()
77
else:
8-
res.pop()
9-
return ''.join(res)
8+
stk.append(c)
9+
return ''.join(stk)

0 commit comments

Comments
 (0)