Longest Nice Substring Visualization & Animation
Finds the longest substring where every letter appears in both cases using divide-and-conquer.
## What is it?
Find the longest substring where every letter appears in both uppercase and lowercase. A "nice" substring is one that is case-complete for all letters it contains.
## How it works
**Divide and Conquer:**
- Find any character in the string that appears in only one case (no counterpart)
- That character cannot appear in any valid nice substring — split the string at that character
- Recursively find the longest nice substring in each part
- Return the longest result across all parts
**Brute Force (O(n²)):**
- Check every substring; for each, verify if it is "nice" using a set
## When to use
- Problems involving character case constraints in substrings
- Divide-and-conquer pattern practice
## Key Points
- Divide-and-conquer runs in O(n²) worst case but is elegant and easy to implement
- A nice substring must contain only characters that have both cases present
- Handles empty strings and single-character strings trivially (return "")
Category: algorithms
Difficulty: beginner
- sliding-window-strings
- strings
Time Complexity: O(n²)
Space Complexity: O(n)
View Longest Nice Substring VisualizationLongest Nice Substring
beginnerFinds the longest substring where every letter appears in both cases using divide-and-conquer.
PhaseInit
K—
Distinct0
Pairs0
Best0
Y
[0]a
[1]z
[2]a
[3]A
[4]a
[5]y
[6]Pair status
Character in window
Nice window / best result
Being evicted (shrink)
Past the left pointer
Input: "YazaAay". Find longest substring where every letter has both cases. Will try k=1..3.
1 / 40