Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 <= 105s consists only of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"leeetcode"
"aaabaaaa"
"aab"
find-maximum-removals-from-source-string)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1957: Delete Characters to Make Fancy String
class Solution {
public String makeFancyString(String s) {
StringBuilder ans = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (i < 2 || c != s.charAt(i - 1) || c != s.charAt(i - 2)) {
ans.append(c);
}
}
return ans.toString();
}
}
// Accepted solution for LeetCode #1957: Delete Characters to Make Fancy String
func makeFancyString(s string) string {
ans := []byte{}
for i, ch := range s {
if c := byte(ch); i < 2 || c != s[i-1] || c != s[i-2] {
ans = append(ans, c)
}
}
return string(ans)
}
# Accepted solution for LeetCode #1957: Delete Characters to Make Fancy String
class Solution:
def makeFancyString(self, s: str) -> str:
ans = []
for i, c in enumerate(s):
if i < 2 or c != s[i - 1] or c != s[i - 2]:
ans.append(c)
return "".join(ans)
// Accepted solution for LeetCode #1957: Delete Characters to Make Fancy String
struct Solution;
impl Solution {
fn make_fancy_string(s: String) -> String {
let mut count = 0;
let mut prev = ' ';
let mut res = "".to_string();
for c in s.chars() {
if c == prev {
if count < 2 {
res.push(c);
}
count += 1;
} else {
res.push(c);
prev = c;
count = 1;
}
}
res
}
}
#[test]
fn test() {
let s = "leeetcode".to_string();
let res = "leetcode".to_string();
assert_eq!(Solution::make_fancy_string(s), res);
let s = "aaabaaaa".to_string();
let res = "aabaa".to_string();
assert_eq!(Solution::make_fancy_string(s), res);
let s = "aab".to_string();
let res = "aab".to_string();
assert_eq!(Solution::make_fancy_string(s), res);
}
// Accepted solution for LeetCode #1957: Delete Characters to Make Fancy String
function makeFancyString(s: string): string {
const ans: string[] = [];
for (let i = 0; i < s.length; ++i) {
if (s[i] !== s[i - 1] || s[i] !== s[i - 2]) {
ans.push(s[i]);
}
}
return ans.join('');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.