Skip to content

1957. Delete Characters to Make Fancy String

Description

A fancy string is a string where no three consecutive characters are equal.

Given a string s, delete the minimum possible number of characters from s to make it fancy.

Return the final string after the deletion. It can be shown that the answer will always be unique.

 

Example 1:

Input: s = "leeetcode"
Output: "leetcode"
Explanation:
Remove an 'e' from the first group of 'e's to create "leetcode".
No three consecutive characters are equal, so return "leetcode".

Example 2:

Input: s = "aaabaaaa"
Output: "aabaa"
Explanation:
Remove an 'a' from the first group of 'a's to create "aabaaaa".
Remove two 'a's from the second group of 'a's to create "aabaa".
No three consecutive characters are equal, so return "aabaa".

Example 3:

Input: s = "aab"
Output: "aab"
Explanation: No three consecutive characters are equal, so return "aab".

 

Constraints:

  • 1 <= s.length <= 105
  • s consists only of lowercase English letters.

Solutions

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

1
2
3
4
5
6
7
class Solution:
    def makeFancyString(self, s: str) -> str:
        ans = []
        for c in s:
            if len(ans) < 2 or ans[-1] != c or ans[-2] != c:
                ans.append(c)
        return "".join(ans)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    public String makeFancyString(String s) {
        StringBuilder ans = new StringBuilder();
        for (char c : s.toCharArray()) {
            int n = ans.length();
            if (n < 2 || c != ans.charAt(n - 1) || c != ans.charAt(n - 2)) {
                ans.append(c);
            }
        }
        return ans.toString();
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
public:
    string makeFancyString(string s) {
        string ans = "";
        for (char& c : s) {
            int n = ans.size();
            if (n < 2 || ans[n - 1] != c || ans[n - 2] != c) {
                ans += c;
            }
        }
        return ans;
    }
};
1
2
3
4
5
6
7
8
9
func makeFancyString(s string) string {
    ans := []rune{}
    for _, c := range s {
        if n := len(ans); n < 2 || c != ans[n-1] || c != ans[n-2] {
            ans = append(ans, c)
        }
    }
    return string(ans)
}
1
2
3
4
5
6
7
8
9
function makeFancyString(s: string): string {
    let [n, ans] = [s.length, ''];
    for (let i = 0; i < n; i++) {
        if (s[i] !== s[i - 1] || s[i] !== s[i - 2]) {
            ans += s[i];
        }
    }
    return ans;
}
1
2
3
4
5
6
7
8
9
function makeFancyString(s) {
    let [n, ans] = [s.length, ''];
    for (let i = 0; i < n; i++) {
        if (s[i] !== s[i - 1] || s[i] !== s[i - 2]) {
            ans += s[i];
        }
    }
    return ans;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    /**
     * @param String $s
     * @return String
     */
    function makeFancyString($s) {
        $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 implode('', $ans);
    }
}

Comments