Skip to content

Commit 95034df

Browse files
committed
feat: add solutions to lc problem: No.1544
No.1544.Make The String Great
1 parent 3d29602 commit 95034df

File tree

6 files changed

+179
-24
lines changed

6 files changed

+179
-24
lines changed

solution/1500-1599/1544.Make The String Great/README.md

+68-1
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,91 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:栈模拟**
65+
66+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是字符串 `s` 的长度。
67+
6468
<!-- tabs:start -->
6569

6670
### **Python3**
6771

6872
<!-- 这里可写当前语言的特殊实现逻辑 -->
6973

7074
```python
71-
75+
class Solution:
76+
def makeGood(self, s: str) -> str:
77+
stk = []
78+
for c in s:
79+
if not stk or abs(ord(stk[-1]) - ord(c)) != 32:
80+
stk.append(c)
81+
else:
82+
stk.pop()
83+
return "".join(stk)
7284
```
7385

7486
### **Java**
7587

7688
<!-- 这里可写当前语言的特殊实现逻辑 -->
7789

7890
```java
91+
class Solution {
92+
public String makeGood(String s) {
93+
StringBuilder sb = new StringBuilder();
94+
for (char c : s.toCharArray()) {
95+
if (sb.length() == 0 || Math.abs(sb.charAt(sb.length() - 1) - c) != 32) {
96+
sb.append(c);
97+
} else {
98+
sb.deleteCharAt(sb.length() - 1);
99+
}
100+
}
101+
return sb.toString();
102+
}
103+
}
104+
```
79105

106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
string makeGood(string s) {
112+
string stk;
113+
for (char c : s) {
114+
if (stk.empty() || abs(stk.back() - c) != 32) {
115+
stk += c;
116+
} else {
117+
stk.pop_back();
118+
}
119+
}
120+
return stk;
121+
}
122+
};
80123
```
81124
125+
### **Go**
126+
127+
```go
128+
func makeGood(s string) string {
129+
stk := []rune{}
130+
for _, c := range s {
131+
if len(stk) == 0 || abs(int(stk[len(stk)-1]-c)) != 32 {
132+
stk = append(stk, c)
133+
} else {
134+
stk = stk[:len(stk)-1]
135+
}
136+
}
137+
return string(stk)
138+
}
139+
140+
func abs(x int) int {
141+
if x < 0 {
142+
return -x
143+
}
144+
return x
145+
}
146+
```
147+
148+
82149
### **...**
83150

84151
```

solution/1500-1599/1544.Make The String Great/README_EN.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,75 @@
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def makeGood(self, s: str) -> str:
65+
stk = []
66+
for c in s:
67+
if not stk or abs(ord(stk[-1]) - ord(c)) != 32:
68+
stk.append(c)
69+
else:
70+
stk.pop()
71+
return "".join(stk)
6472
```
6573

6674
### **Java**
6775

6876
```java
77+
class Solution {
78+
public String makeGood(String s) {
79+
StringBuilder sb = new StringBuilder();
80+
for (char c : s.toCharArray()) {
81+
if (sb.length() == 0 || Math.abs(sb.charAt(sb.length() - 1) - c) != 32) {
82+
sb.append(c);
83+
} else {
84+
sb.deleteCharAt(sb.length() - 1);
85+
}
86+
}
87+
return sb.toString();
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
string makeGood(string s) {
98+
string stk;
99+
for (char c : s) {
100+
if (stk.empty() || abs(stk.back() - c) != 32) {
101+
stk += c;
102+
} else {
103+
stk.pop_back();
104+
}
105+
}
106+
return stk;
107+
}
108+
};
109+
```
69110
111+
### **Go**
112+
113+
```go
114+
func makeGood(s string) string {
115+
stk := []rune{}
116+
for _, c := range s {
117+
if len(stk) == 0 || abs(int(stk[len(stk)-1]-c)) != 32 {
118+
stk = append(stk, c)
119+
} else {
120+
stk = stk[:len(stk)-1]
121+
}
122+
}
123+
return string(stk)
124+
}
125+
126+
func abs(x int) int {
127+
if x < 0 {
128+
return -x
129+
}
130+
return x
131+
}
70132
```
71133

72134
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
string makeGood(string s) {
4+
string stk;
5+
for (char c : s) {
6+
if (stk.empty() || abs(stk.back() - c) != 32) {
7+
stk += c;
8+
} else {
9+
stk.pop_back();
10+
}
11+
}
12+
return stk;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func makeGood(s string) string {
2+
stk := []rune{}
3+
for _, c := range s {
4+
if len(stk) == 0 || abs(int(stk[len(stk)-1]-c)) != 32 {
5+
stk = append(stk, c)
6+
} else {
7+
stk = stk[:len(stk)-1]
8+
}
9+
}
10+
return string(stk)
11+
}
12+
13+
func abs(x int) int {
14+
if x < 0 {
15+
return -x
16+
}
17+
return x
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
class Solution {
22
public String makeGood(String s) {
3-
return help(s);
4-
}
5-
6-
private String help(String s) {
7-
if (s == null || s.length() == 0 || s.length() == 1) {
8-
return s;
9-
}
10-
char[] chars = s.toCharArray();
11-
for (int i = 1; i < chars.length; i++) {
12-
if (Math.abs(chars[i] - chars[i - 1]) == Math.abs('A' - 'a')) {
13-
return help(newString(chars, i));
14-
}
15-
}
16-
return s;
17-
}
18-
19-
private String newString(char[] chars, int i) {
20-
StringBuilder tmp = new StringBuilder();
21-
for (int j = 0; j < chars.length; j++) {
22-
if (j != i && j != i - 1) {
23-
tmp.append(chars[j]);
3+
StringBuilder sb = new StringBuilder();
4+
for (char c : s.toCharArray()) {
5+
if (sb.length() == 0 || Math.abs(sb.charAt(sb.length() - 1) - c) != 32) {
6+
sb.append(c);
7+
} else {
8+
sb.deleteCharAt(sb.length() - 1);
249
}
2510
}
26-
return tmp.toString();
11+
return sb.toString();
2712
}
2813
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def makeGood(self, s: str) -> str:
3+
stk = []
4+
for c in s:
5+
if not stk or abs(ord(stk[-1]) - ord(c)) != 32:
6+
stk.append(c)
7+
else:
8+
stk.pop()
9+
return "".join(stk)

0 commit comments

Comments
 (0)