Skip to main content
Back to problems
#1957
Easy Algorithms

Delete characters to make fancy string

String
74.1% acceptance
Feb 25, 2026
1177
54
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn make_fancy_string(s: String) -> String {
    let mut result = String::new();
    let mut count = 0;
    let mut prev = '\0';
    for ch in s.chars() {
      if ch == prev {
        count += 1;
      } else {
        count = 1;
        prev = ch;
      }
      if count <= 2 {
        result.push(ch);
      }
    }
    result
  }
}