1957. Delete Characters to Make Fancy String
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.
Solution:
class Solution {
public String makeFancyString(String s) {
if (s.length() <= 1){
return s;
}
StringBuilder sb = new StringBuilder();
int slow = 0;
/*
a a a b a a a
s
f
sb a s f
*/
int fast = 1;
sb.append(s.charAt(slow));
slow++;
fast++;
while (fast < s.length()){
char curLast = sb.charAt(sb.length() - 1);
while (fast < s.length() && curLast == s.charAt(slow) && curLast == s.charAt(fast)){
fast++;
}
sb.append(s.charAt(slow));
slow = fast;
fast++;
}
if (slow == s.length() - 1){
sb.append(s.charAt(slow));
}
return sb.toString();
}
}
// TC: O(n)
// SC: O(1)