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.This problem asks us to modify a given string s
such that no three consecutive characters are identical. The goal is to achieve this by removing the minimum number of characters.
The most efficient approach is to iteratively build a new string (ans
), adding characters from the input string s
only if they don't violate the "fancy string" condition.
We maintain a check on the last two characters added to ans
. If the current character from s
is the same as the last two, we skip it; otherwise, we append it to ans
.
Initialization: Create an empty string ans
to store the result.
Iteration: Iterate through each character c
in the input string s
.
Conditional Appending:
ans
has fewer than 2 characters, or if the last character of ans
is different from c
, or if the second-to-last character of ans
is different from c
, then append c
to ans
.Return: After processing all characters, return ans
.
s
. We iterate through the string once.Python
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)
Java
class Solution {
public String makeFancyString(String s) {
StringBuilder ans = new StringBuilder();
for (char c : s.toCharArray()) {
int n = ans.length();
if (n < 2 || ans.charAt(n - 1) != c || (n >= 2 && ans.charAt(n - 2) != c)) {
ans.append(c);
}
}
return ans.toString();
}
}
C++
class Solution {
public:
string makeFancyString(string s) {
string ans = "";
for (char c : s) {
if (ans.length() < 2 || ans.back() != c || ans.length() >= 2 && ans[ans.length() - 2] != c) {
ans += c;
}
}
return ans;
}
};
JavaScript
var makeFancyString = function(s) {
let result = "";
for (let i = 0; i < s.length; i++) {
if (result.length < 2 || result[result.length - 1] !== s[i] || result[result.length - 2] !== s[i]) {
result += s[i];
}
}
return result;
};
Go
func makeFancyString(s string) string {
ans := []rune{}
for _, r := range s {
if len(ans) < 2 || ans[len(ans)-1] != r || (len(ans) >= 2 && ans[len(ans)-2] != r) {
ans = append(ans, r)
}
}
return string(ans)
}
These implementations all follow the same basic algorithm, demonstrating its simplicity and efficiency across different programming languages. They all achieve a linear time complexity, making them suitable for handling large input strings.