Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.1957 #3547

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

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

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

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。

<!-- tabs:start -->

Expand All @@ -81,10 +87,9 @@ class Solution:
def makeFancyString(self, s: str) -> str:
ans = []
for c in s:
if len(ans) > 1 and ans[-1] == ans[-2] == c:
continue
ans.append(c)
return ''.join(ans)
if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
ans.append(c)
return "".join(ans)
```

#### Java
Expand All @@ -95,10 +100,9 @@ class Solution {
StringBuilder ans = new StringBuilder();
for (char c : s.toCharArray()) {
int n = ans.length();
if (n > 1 && ans.charAt(n - 1) == c && ans.charAt(n - 2) == c) {
continue;
if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
ans.append(c);
}
ans.append(c);
}
return ans.toString();
}
Expand All @@ -114,8 +118,9 @@ public:
string ans = "";
for (char& c : s) {
int n = ans.size();
if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue;
ans.push_back(c);
if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
ans += c;
}
}
return ans;
}
Expand All @@ -128,16 +133,29 @@ public:
func makeFancyString(s string) string {
ans := []rune{}
for _, c := range s {
n := len(ans)
if n > 1 && ans[n-1] == c && ans[n-2] == c {
continue
if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
ans = append(ans, c)
}
ans = append(ans, c)
}
return string(ans)
}
```

#### TypeScript

```ts
function makeFancyString(s: string): string {
const ans: string[] = [];
for (const c of s) {
const n = ans.length;
if (n < 2 || c !== ans[n - 1] || c !== ans[n - 2]) {
ans.push(c);
}
}
return ans.join('');
}
```

#### PHP

```php
Expand All @@ -147,15 +165,17 @@ class Solution {
* @return String
*/
function makeFancyString($s) {
$rs = '';
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] == $s[$i + 1] && $s[$i] == $s[$i + 2]) {
continue;
} else {
$rs .= $s[$i];
$ans = [];
$length = strlen($s);

for ($i = 0; $i < $length; $i++) {
$n = count($ans);
if ($n < 2 || $s[$i] !== $ans[$n - 1] || $s[$i] !== $ans[$n - 2]) {
$ans[] = $s[$i];
}
}
return $rs;

return implode('', $ans);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ No three consecutive characters are equal, so return &quot;aabaa&quot;.

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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}$.

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

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.

<!-- tabs:start -->

Expand All @@ -79,10 +85,9 @@ class Solution:
def makeFancyString(self, s: str) -> str:
ans = []
for c in s:
if len(ans) > 1 and ans[-1] == ans[-2] == c:
continue
ans.append(c)
return ''.join(ans)
if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
ans.append(c)
return "".join(ans)
```

#### Java
Expand All @@ -93,10 +98,9 @@ class Solution {
StringBuilder ans = new StringBuilder();
for (char c : s.toCharArray()) {
int n = ans.length();
if (n > 1 && ans.charAt(n - 1) == c && ans.charAt(n - 2) == c) {
continue;
if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
ans.append(c);
}
ans.append(c);
}
return ans.toString();
}
Expand All @@ -112,8 +116,9 @@ public:
string ans = "";
for (char& c : s) {
int n = ans.size();
if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue;
ans.push_back(c);
if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
ans += c;
}
}
return ans;
}
Expand All @@ -126,16 +131,29 @@ public:
func makeFancyString(s string) string {
ans := []rune{}
for _, c := range s {
n := len(ans)
if n > 1 && ans[n-1] == c && ans[n-2] == c {
continue
if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
ans = append(ans, c)
}
ans = append(ans, c)
}
return string(ans)
}
```

#### TypeScript

```ts
function makeFancyString(s: string): string {
const ans: string[] = [];
for (const c of s) {
const n = ans.length;
if (n < 2 || c !== ans[n - 1] || c !== ans[n - 2]) {
ans.push(c);
}
}
return ans.join('');
}
```

#### PHP

```php
Expand All @@ -145,15 +163,17 @@ class Solution {
* @return String
*/
function makeFancyString($s) {
$rs = '';
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] == $s[$i + 1] && $s[$i] == $s[$i + 2]) {
continue;
} else {
$rs .= $s[$i];
$ans = [];
$length = strlen($s);

for ($i = 0; $i < $length; $i++) {
$n = count($ans);
if ($n < 2 || $s[$i] !== $ans[$n - 1] || $s[$i] !== $ans[$n - 2]) {
$ans[] = $s[$i];
}
}
return $rs;

return implode('', $ans);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ class Solution {
string ans = "";
for (char& c : s) {
int n = ans.size();
if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue;
ans.push_back(c);
if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
ans += c;
}
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
func makeFancyString(s string) string {
ans := []rune{}
for _, c := range s {
n := len(ans)
if n > 1 && ans[n-1] == c && ans[n-2] == c {
continue
if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
ans = append(ans, c)
}
ans = append(ans, c)
}
return string(ans)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ public String makeFancyString(String s) {
StringBuilder ans = new StringBuilder();
for (char c : s.toCharArray()) {
int n = ans.length();
if (n > 1 && ans.charAt(n - 1) == c && ans.charAt(n - 2) == c) {
continue;
if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
ans.append(c);
}
ans.append(c);
}
return ans.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ class Solution {
* @return String
*/
function makeFancyString($s) {
$rs = '';
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] == $s[$i + 1] && $s[$i] == $s[$i + 2]) {
continue;
} else {
$rs .= $s[$i];
$ans = [];
$length = strlen($s);

for ($i = 0; $i < $length; $i++) {
$n = count($ans);
if ($n < 2 || $s[$i] !== $ans[$n - 1] || $s[$i] !== $ans[$n - 2]) {
$ans[] = $s[$i];
}
}
return $rs;

return implode('', $ans);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class Solution:
def makeFancyString(self, s: str) -> str:
ans = []
for c in s:
if len(ans) > 1 and ans[-1] == ans[-2] == c:
continue
ans.append(c)
return ''.join(ans)
if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
ans.append(c)
return "".join(ans)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function makeFancyString(s: string): string {
const ans: string[] = [];
for (const c of s) {
const n = ans.length;
if (n < 2 || c !== ans[n - 1] || c !== ans[n - 2]) {
ans.push(c);
}
}
return ans.join('');
}
Loading