File: delete-characters-to-make-fancy-string/solution.py

Date: 2026-06-06

Time: 16:14

Purpose

This file solves LeetCode 1957 — Delete Characters to Make Fancy String. A "fancy string" is one where no three consecutive characters are the same. The solution removes the minimum number of characters to achieve this property.

It follows the repo's standard layout: each problem gets a directory with solution.py, test_solution.py, plan.md, and review.md.

Key Components

Solution.makeFancyString(self, s: str) -> str

The single method. Contract:

Patterns

Greedy single-pass with output stack. The result list acts as a stack where only the tail matters. Each character is either appended or skipped based on the two most recent entries. This is idiomatic for problems where a local decision (keep/skip) depends on a bounded window of prior output.

The continue-to-skip / default-to-append structure avoids an else branch — the append always runs unless the continue fires. This is a common Python idiom for filter-style loops.

Dependencies

Imports: None — pure standard library, no external dependencies.

Imported by: delete-characters-to-make-fancy-string/test_solution.py directly. The "Imported By" list in the prompt is misleading — those 400+ test files are likely an artifact of the repo's test infrastructure importing a shared test runner or Solution base, not this specific file.

Flow

1. Initialize empty list result

2. Iterate through each character c in s

3. Check: if result has at least 2 elements AND both result[-1] and result[-2] equal c → skip (this would create a triple)

4. Otherwise → append c

5. Join and return

The len(result) >= 2 guard prevents index errors on the first two characters, which are always safe to append.

Invariants

Error Handling

None. The function assumes valid input per LeetCode constraints. An empty string produces an empty result naturally (the loop body never executes). A single-character or two-character string passes through unchanged since the len(result) >= 2 guard prevents the skip condition from ever firing.

Topics to Explore

Beliefs