[LeetCode] Delete Characters to Make Fancy String

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
string makeFancyString(string s) {
string res = "";
for(auto& ch : s) {
if(res.size() >= 2 and res.back() == ch and res[res.size()-2] == ch) continue;
res.push_back(ch);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/11/02/PS/LeetCode/delete-characters-to-make-fancy-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.