classSolution { public: string makeFancyString(string s){ string ans; char last = '0'; int cnt = 0; for (char c : s) { if (c != last) { last = c; cnt = 0; } cnt++; if (cnt < 3) { ans += c; } } return ans; } };
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
''' Author: LetMeFly Date: 2025-07-21 18:48:15 LastEditors: LetMeFly.xyz LastEditTime: 2025-07-21 18:54:53 ''' classSolution: defmakeFancyString(self, s: str) -> str: ans = [] last = '0' cnt = 0 for c in s: if c != last: last = c cnt = 0 cnt += 1 if cnt < 3: ans.append(c) return''.join(ans)
funcmakeFancyString(s string)string { ans := []byte{} last := byte(0) cnt := 0 for _, c := range s { ifbyte(c) != last { last = byte(c) cnt = 0 } cnt++ if cnt < 3 { ans = append(ans, byte(c)) } } returnstring(ans) }