Skip to content

Commit 2604b21

Browse files
authored
feat: update solutions to lc problem: No.1957 (#4587)
No.1957.Delete Characters to Make Fancy String
1 parent 0fe1388 commit 2604b21

File tree

9 files changed

+99
-96
lines changed

9 files changed

+99
-96
lines changed

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

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ tags:
7272

7373
### 方法一:模拟
7474

75-
我们可以遍历字符串 $s$,并使用一个数组 $\textit{ans}$ 记录当前的答案。对于每一个字符 $c$,如果 $\textit{ans}$ 的长度小于 $2$ 或者 $\textit{ans}$ 的最后两个字符不等于 $c$,我们就将 $c$ 添加到 $\textit{ans}$ 中。
75+
我们可以遍历字符串 $s$,并使用一个数组 $\textit{ans}$ 记录当前的答案。对于每一个字符 $\textit{s[i]}$,如果 $i \lt 2$ 或者 $s[i]$ 与 $s[i - 1]$ 不等,或者 $s[i]$ 与 $s[i - 2]$ 不等,我们就将 $s[i]$ 添加到 $\textit{ans}$ 中。
7676

7777
最后,我们将 $\textit{ans}$ 中的字符连接起来,就得到了答案。
7878

79-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
79+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$
8080

8181
<!-- tabs:start -->
8282

@@ -86,8 +86,8 @@ tags:
8686
class Solution:
8787
def makeFancyString(self, s: str) -> str:
8888
ans = []
89-
for c in s:
90-
if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
89+
for i, c in enumerate(s):
90+
if i < 2 or c != s[i - 1] or c != s[i - 2]:
9191
ans.append(c)
9292
return "".join(ans)
9393
```
@@ -98,9 +98,9 @@ class Solution:
9898
class Solution {
9999
public String makeFancyString(String s) {
100100
StringBuilder ans = new StringBuilder();
101-
for (char c : s.toCharArray()) {
102-
int n = ans.length();
103-
if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
101+
for (int i = 0; i < s.length(); ++i) {
102+
char c = s.charAt(i);
103+
if (i < 2 || c != s.charAt(i - 1) || c != s.charAt(i - 2)) {
104104
ans.append(c);
105105
}
106106
}
@@ -116,9 +116,9 @@ class Solution {
116116
public:
117117
string makeFancyString(string s) {
118118
string ans = "";
119-
for (char& c : s) {
120-
int n = ans.size();
121-
if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
119+
for (int i = 0; i < s.length(); ++i) {
120+
char c = s[i];
121+
if (i < 2 || c != s[i - 1] || c != s[i - 2]) {
122122
ans += c;
123123
}
124124
}
@@ -131,9 +131,9 @@ public:
131131
132132
```go
133133
func makeFancyString(s string) string {
134-
ans := []rune{}
135-
for _, c := range s {
136-
if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
134+
ans := []byte{}
135+
for i, ch := range s {
136+
if c := byte(ch); i < 2 || c != s[i-1] || c != s[i-2] {
137137
ans = append(ans, c)
138138
}
139139
}
@@ -145,28 +145,32 @@ func makeFancyString(s string) string {
145145

146146
```ts
147147
function makeFancyString(s: string): string {
148-
let [n, ans] = [s.length, ''];
149-
for (let i = 0; i < n; i++) {
148+
const ans: string[] = [];
149+
for (let i = 0; i < s.length; ++i) {
150150
if (s[i] !== s[i - 1] || s[i] !== s[i - 2]) {
151-
ans += s[i];
151+
ans.push(s[i]);
152152
}
153153
}
154-
return ans;
154+
return ans.join('');
155155
}
156156
```
157157

158158
#### JavaScript
159159

160160
```js
161-
function makeFancyString(s) {
162-
let [n, ans] = [s.length, ''];
163-
for (let i = 0; i < n; i++) {
161+
/**
162+
* @param {string} s
163+
* @return {string}
164+
*/
165+
var makeFancyString = function (s) {
166+
const ans = [];
167+
for (let i = 0; i < s.length; ++i) {
164168
if (s[i] !== s[i - 1] || s[i] !== s[i - 2]) {
165-
ans += s[i];
169+
ans.push(s[i]);
166170
}
167171
}
168-
return ans;
169-
}
172+
return ans.join('');
173+
};
170174
```
171175

172176
#### PHP
@@ -178,17 +182,14 @@ class Solution {
178182
* @return String
179183
*/
180184
function makeFancyString($s) {
181-
$ans = [];
182-
$length = strlen($s);
183-
184-
for ($i = 0; $i < $length; $i++) {
185-
$n = count($ans);
186-
if ($n < 2 || $s[$i] !== $ans[$n - 1] || $s[$i] !== $ans[$n - 2]) {
187-
$ans[] = $s[$i];
185+
$ans = '';
186+
for ($i = 0; $i < strlen($s); $i++) {
187+
$c = $s[$i];
188+
if ($i < 2 || $c !== $s[$i - 1] || $c !== $s[$i - 2]) {
189+
$ans .= $c;
188190
}
189191
}
190-
191-
return implode('', $ans);
192+
return $ans;
192193
}
193194
}
194195
```

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

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ No three consecutive characters are equal, so return &quot;aabaa&quot;.
7070

7171
### Solution 1: Simulation
7272

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}$.
73+
We can iterate through the string $s$ and use an array $\textit{ans}$ to record the current answer. For each character $\textit{s[i]}$, if $i < 2$ or $s[i]$ is not equal to $s[i - 1]$, or $s[i]$ is not equal to $s[i - 2]$, we add $s[i]$ to $\textit{ans}$.
7474

7575
Finally, we concatenate the characters in $\textit{ans}$ to get the answer.
7676

77-
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.
77+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
7878

7979
<!-- tabs:start -->
8080

@@ -84,8 +84,8 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
8484
class Solution:
8585
def makeFancyString(self, s: str) -> str:
8686
ans = []
87-
for c in s:
88-
if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
87+
for i, c in enumerate(s):
88+
if i < 2 or c != s[i - 1] or c != s[i - 2]:
8989
ans.append(c)
9090
return "".join(ans)
9191
```
@@ -96,9 +96,9 @@ class Solution:
9696
class Solution {
9797
public String makeFancyString(String s) {
9898
StringBuilder ans = new StringBuilder();
99-
for (char c : s.toCharArray()) {
100-
int n = ans.length();
101-
if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
99+
for (int i = 0; i < s.length(); ++i) {
100+
char c = s.charAt(i);
101+
if (i < 2 || c != s.charAt(i - 1) || c != s.charAt(i - 2)) {
102102
ans.append(c);
103103
}
104104
}
@@ -114,9 +114,9 @@ class Solution {
114114
public:
115115
string makeFancyString(string s) {
116116
string ans = "";
117-
for (char& c : s) {
118-
int n = ans.size();
119-
if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
117+
for (int i = 0; i < s.length(); ++i) {
118+
char c = s[i];
119+
if (i < 2 || c != s[i - 1] || c != s[i - 2]) {
120120
ans += c;
121121
}
122122
}
@@ -129,9 +129,9 @@ public:
129129
130130
```go
131131
func makeFancyString(s string) string {
132-
ans := []rune{}
133-
for _, c := range s {
134-
if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
132+
ans := []byte{}
133+
for i, ch := range s {
134+
if c := byte(ch); i < 2 || c != s[i-1] || c != s[i-2] {
135135
ans = append(ans, c)
136136
}
137137
}
@@ -143,28 +143,32 @@ func makeFancyString(s string) string {
143143

144144
```ts
145145
function makeFancyString(s: string): string {
146-
let [n, ans] = [s.length, ''];
147-
for (let i = 0; i < n; i++) {
146+
const ans: string[] = [];
147+
for (let i = 0; i < s.length; ++i) {
148148
if (s[i] !== s[i - 1] || s[i] !== s[i - 2]) {
149-
ans += s[i];
149+
ans.push(s[i]);
150150
}
151151
}
152-
return ans;
152+
return ans.join('');
153153
}
154154
```
155155

156156
#### JavaScript
157157

158158
```js
159-
function makeFancyString(s) {
160-
let [n, ans] = [s.length, ''];
161-
for (let i = 0; i < n; i++) {
159+
/**
160+
* @param {string} s
161+
* @return {string}
162+
*/
163+
var makeFancyString = function (s) {
164+
const ans = [];
165+
for (let i = 0; i < s.length; ++i) {
162166
if (s[i] !== s[i - 1] || s[i] !== s[i - 2]) {
163-
ans += s[i];
167+
ans.push(s[i]);
164168
}
165169
}
166-
return ans;
167-
}
170+
return ans.join('');
171+
};
168172
```
169173

170174
#### PHP
@@ -176,17 +180,14 @@ class Solution {
176180
* @return String
177181
*/
178182
function makeFancyString($s) {
179-
$ans = [];
180-
$length = strlen($s);
181-
182-
for ($i = 0; $i < $length; $i++) {
183-
$n = count($ans);
184-
if ($n < 2 || $s[$i] !== $ans[$n - 1] || $s[$i] !== $ans[$n - 2]) {
185-
$ans[] = $s[$i];
183+
$ans = '';
184+
for ($i = 0; $i < strlen($s); $i++) {
185+
$c = $s[$i];
186+
if ($i < 2 || $c !== $s[$i - 1] || $c !== $s[$i - 2]) {
187+
$ans .= $c;
186188
}
187189
}
188-
189-
return implode('', $ans);
190+
return $ans;
190191
}
191192
}
192193
```

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ class Solution {
22
public:
33
string makeFancyString(string s) {
44
string ans = "";
5-
for (char& c : s) {
6-
int n = ans.size();
7-
if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
5+
for (int i = 0; i < s.length(); ++i) {
6+
char c = s[i];
7+
if (i < 2 || c != s[i - 1] || c != s[i - 2]) {
88
ans += c;
99
}
1010
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
func makeFancyString(s string) string {
2-
ans := []rune{}
3-
for _, c := range s {
4-
if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
2+
ans := []byte{}
3+
for i, ch := range s {
4+
if c := byte(ch); i < 2 || c != s[i-1] || c != s[i-2] {
55
ans = append(ans, c)
66
}
77
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public String makeFancyString(String s) {
33
StringBuilder ans = new StringBuilder();
4-
for (char c : s.toCharArray()) {
5-
int n = ans.length();
6-
if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
4+
for (int i = 0; i < s.length(); ++i) {
5+
char c = s.charAt(i);
6+
if (i < 2 || c != s.charAt(i - 1) || c != s.charAt(i - 2)) {
77
ans.append(c);
88
}
99
}
1010
return ans.toString();
1111
}
12-
}
12+
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
function makeFancyString(s) {
2-
let [n, ans] = [s.length, ''];
3-
for (let i = 0; i < n; i++) {
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
var makeFancyString = function (s) {
6+
const ans = [];
7+
for (let i = 0; i < s.length; ++i) {
48
if (s[i] !== s[i - 1] || s[i] !== s[i - 2]) {
5-
ans += s[i];
9+
ans.push(s[i]);
610
}
711
}
8-
return ans;
9-
}
12+
return ans.join('');
13+
};

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ class Solution {
44
* @return String
55
*/
66
function makeFancyString($s) {
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];
7+
$ans = '';
8+
for ($i = 0; $i < strlen($s); $i++) {
9+
$c = $s[$i];
10+
if ($i < 2 || $c !== $s[$i - 1] || $c !== $s[$i - 2]) {
11+
$ans .= $c;
1412
}
1513
}
16-
17-
return implode('', $ans);
14+
return $ans;
1815
}
19-
}
16+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def makeFancyString(self, s: str) -> str:
33
ans = []
4-
for c in s:
5-
if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
4+
for i, c in enumerate(s):
5+
if i < 2 or c != s[i - 1] or c != s[i - 2]:
66
ans.append(c)
77
return "".join(ans)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function makeFancyString(s: string): string {
2-
let [n, ans] = [s.length, ''];
3-
for (let i = 0; i < n; i++) {
2+
const ans: string[] = [];
3+
for (let i = 0; i < s.length; ++i) {
44
if (s[i] !== s[i - 1] || s[i] !== s[i - 2]) {
5-
ans += s[i];
5+
ans.push(s[i]);
66
}
77
}
8-
return ans;
8+
return ans.join('');
99
}

0 commit comments

Comments
 (0)