Skip to content

Commit 1705e6b

Browse files
authored
feat: add solutions to lc problem: No.1957 (#3547)
No.1957.Delete Characters to Make Fancy String
1 parent 9559060 commit 1705e6b

File tree

8 files changed

+114
-65
lines changed

8 files changed

+114
-65
lines changed

solution/1900-1999/1957.Delete Characters to Make Fancy String/README.md

+41-21
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ tags:
7070

7171
<!-- solution:start -->
7272

73-
### 方法一
73+
### 方法一:模拟
74+
75+
我们可以遍历字符串 $s$,并使用一个数组 $\textit{ans}$ 记录当前的答案。对于每一个字符 $c$,如果 $\textit{ans}$ 的长度小于 $2$ 或者 $\textit{ans}$ 的最后两个字符不等于 $c$,我们就将 $c$ 添加到 $\textit{ans}$ 中。
76+
77+
最后,我们将 $\textit{ans}$ 中的字符连接起来,就得到了答案。
78+
79+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
7480

7581
<!-- tabs:start -->
7682

@@ -81,10 +87,9 @@ class Solution:
8187
def makeFancyString(self, s: str) -> str:
8288
ans = []
8389
for c in s:
84-
if len(ans) > 1 and ans[-1] == ans[-2] == c:
85-
continue
86-
ans.append(c)
87-
return ''.join(ans)
90+
if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
91+
ans.append(c)
92+
return "".join(ans)
8893
```
8994

9095
#### Java
@@ -95,10 +100,9 @@ class Solution {
95100
StringBuilder ans = new StringBuilder();
96101
for (char c : s.toCharArray()) {
97102
int n = ans.length();
98-
if (n > 1 && ans.charAt(n - 1) == c && ans.charAt(n - 2) == c) {
99-
continue;
103+
if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
104+
ans.append(c);
100105
}
101-
ans.append(c);
102106
}
103107
return ans.toString();
104108
}
@@ -114,8 +118,9 @@ public:
114118
string ans = "";
115119
for (char& c : s) {
116120
int n = ans.size();
117-
if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue;
118-
ans.push_back(c);
121+
if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
122+
ans += c;
123+
}
119124
}
120125
return ans;
121126
}
@@ -128,16 +133,29 @@ public:
128133
func makeFancyString(s string) string {
129134
ans := []rune{}
130135
for _, c := range s {
131-
n := len(ans)
132-
if n > 1 && ans[n-1] == c && ans[n-2] == c {
133-
continue
136+
if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
137+
ans = append(ans, c)
134138
}
135-
ans = append(ans, c)
136139
}
137140
return string(ans)
138141
}
139142
```
140143

144+
#### TypeScript
145+
146+
```ts
147+
function makeFancyString(s: string): string {
148+
const ans: string[] = [];
149+
for (const c of s) {
150+
const n = ans.length;
151+
if (n < 2 || c !== ans[n - 1] || c !== ans[n - 2]) {
152+
ans.push(c);
153+
}
154+
}
155+
return ans.join('');
156+
}
157+
```
158+
141159
#### PHP
142160

143161
```php
@@ -147,15 +165,17 @@ class Solution {
147165
* @return String
148166
*/
149167
function makeFancyString($s) {
150-
$rs = '';
151-
for ($i = 0; $i < strlen($s); $i++) {
152-
if ($s[$i] == $s[$i + 1] && $s[$i] == $s[$i + 2]) {
153-
continue;
154-
} else {
155-
$rs .= $s[$i];
168+
$ans = [];
169+
$length = strlen($s);
170+
171+
for ($i = 0; $i < $length; $i++) {
172+
$n = count($ans);
173+
if ($n < 2 || $s[$i] !== $ans[$n - 1] || $s[$i] !== $ans[$n - 2]) {
174+
$ans[] = $s[$i];
156175
}
157176
}
158-
return $rs;
177+
178+
return implode('', $ans);
159179
}
160180
}
161181
```

solution/1900-1999/1957.Delete Characters to Make Fancy String/README_EN.md

+41-21
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ No three consecutive characters are equal, so return &quot;aabaa&quot;.
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Simulation
72+
73+
We can traverse the string $s$ and use an array $\textit{ans}$ to record the current answer. For each character $c$, if the length of $\textit{ans}$ is less than $2$ or the last two characters of $\textit{ans}$ are not equal to $c$, we add $c$ to $\textit{ans}$.
74+
75+
Finally, we concatenate the characters in $\textit{ans}$ to get the answer.
76+
77+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.
7278

7379
<!-- tabs:start -->
7480

@@ -79,10 +85,9 @@ class Solution:
7985
def makeFancyString(self, s: str) -> str:
8086
ans = []
8187
for c in s:
82-
if len(ans) > 1 and ans[-1] == ans[-2] == c:
83-
continue
84-
ans.append(c)
85-
return ''.join(ans)
88+
if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
89+
ans.append(c)
90+
return "".join(ans)
8691
```
8792

8893
#### Java
@@ -93,10 +98,9 @@ class Solution {
9398
StringBuilder ans = new StringBuilder();
9499
for (char c : s.toCharArray()) {
95100
int n = ans.length();
96-
if (n > 1 && ans.charAt(n - 1) == c && ans.charAt(n - 2) == c) {
97-
continue;
101+
if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
102+
ans.append(c);
98103
}
99-
ans.append(c);
100104
}
101105
return ans.toString();
102106
}
@@ -112,8 +116,9 @@ public:
112116
string ans = "";
113117
for (char& c : s) {
114118
int n = ans.size();
115-
if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue;
116-
ans.push_back(c);
119+
if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
120+
ans += c;
121+
}
117122
}
118123
return ans;
119124
}
@@ -126,16 +131,29 @@ public:
126131
func makeFancyString(s string) string {
127132
ans := []rune{}
128133
for _, c := range s {
129-
n := len(ans)
130-
if n > 1 && ans[n-1] == c && ans[n-2] == c {
131-
continue
134+
if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
135+
ans = append(ans, c)
132136
}
133-
ans = append(ans, c)
134137
}
135138
return string(ans)
136139
}
137140
```
138141

142+
#### TypeScript
143+
144+
```ts
145+
function makeFancyString(s: string): string {
146+
const ans: string[] = [];
147+
for (const c of s) {
148+
const n = ans.length;
149+
if (n < 2 || c !== ans[n - 1] || c !== ans[n - 2]) {
150+
ans.push(c);
151+
}
152+
}
153+
return ans.join('');
154+
}
155+
```
156+
139157
#### PHP
140158

141159
```php
@@ -145,15 +163,17 @@ class Solution {
145163
* @return String
146164
*/
147165
function makeFancyString($s) {
148-
$rs = '';
149-
for ($i = 0; $i < strlen($s); $i++) {
150-
if ($s[$i] == $s[$i + 1] && $s[$i] == $s[$i + 2]) {
151-
continue;
152-
} else {
153-
$rs .= $s[$i];
166+
$ans = [];
167+
$length = strlen($s);
168+
169+
for ($i = 0; $i < $length; $i++) {
170+
$n = count($ans);
171+
if ($n < 2 || $s[$i] !== $ans[$n - 1] || $s[$i] !== $ans[$n - 2]) {
172+
$ans[] = $s[$i];
154173
}
155174
}
156-
return $rs;
175+
176+
return implode('', $ans);
157177
}
158178
}
159179
```

solution/1900-1999/1957.Delete Characters to Make Fancy String/Solution.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ class Solution {
44
string ans = "";
55
for (char& c : s) {
66
int n = ans.size();
7-
if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue;
8-
ans.push_back(c);
7+
if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
8+
ans += c;
9+
}
910
}
1011
return ans;
1112
}
12-
};
13+
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
func makeFancyString(s string) string {
22
ans := []rune{}
33
for _, c := range s {
4-
n := len(ans)
5-
if n > 1 && ans[n-1] == c && ans[n-2] == c {
6-
continue
4+
if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
5+
ans = append(ans, c)
76
}
8-
ans = append(ans, c)
97
}
108
return string(ans)
11-
}
9+
}

solution/1900-1999/1957.Delete Characters to Make Fancy String/Solution.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ public String makeFancyString(String s) {
33
StringBuilder ans = new StringBuilder();
44
for (char c : s.toCharArray()) {
55
int n = ans.length();
6-
if (n > 1 && ans.charAt(n - 1) == c && ans.charAt(n - 2) == c) {
7-
continue;
6+
if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
7+
ans.append(c);
88
}
9-
ans.append(c);
109
}
1110
return ans.toString();
1211
}
13-
}
12+
}

solution/1900-1999/1957.Delete Characters to Make Fancy String/Solution.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ class Solution {
44
* @return String
55
*/
66
function makeFancyString($s) {
7-
$rs = '';
8-
for ($i = 0; $i < strlen($s); $i++) {
9-
if ($s[$i] == $s[$i + 1] && $s[$i] == $s[$i + 2]) {
10-
continue;
11-
} else {
12-
$rs .= $s[$i];
7+
$ans = [];
8+
$length = strlen($s);
9+
10+
for ($i = 0; $i < $length; $i++) {
11+
$n = count($ans);
12+
if ($n < 2 || $s[$i] !== $ans[$n - 1] || $s[$i] !== $ans[$n - 2]) {
13+
$ans[] = $s[$i];
1314
}
1415
}
15-
return $rs;
16+
17+
return implode('', $ans);
1618
}
1719
}

solution/1900-1999/1957.Delete Characters to Make Fancy String/Solution.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ class Solution:
22
def makeFancyString(self, s: str) -> str:
33
ans = []
44
for c in s:
5-
if len(ans) > 1 and ans[-1] == ans[-2] == c:
6-
continue
7-
ans.append(c)
8-
return ''.join(ans)
5+
if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
6+
ans.append(c)
7+
return "".join(ans)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function makeFancyString(s: string): string {
2+
const ans: string[] = [];
3+
for (const c of s) {
4+
const n = ans.length;
5+
if (n < 2 || c !== ans[n - 1] || c !== ans[n - 2]) {
6+
ans.push(c);
7+
}
8+
}
9+
return ans.join('');
10+
}

0 commit comments

Comments
 (0)