Make The String Great - Problem

Given a string s containing both lowercase and uppercase English letters, your task is to make it "great" by removing problematic character pairs.

A great string is one where no two adjacent characters are the same letter in different cases (like 'a' next to 'A' or 'B' next to 'b'). These problematic pairs create bad adjacencies that need to be eliminated.

The Process: You can repeatedly choose any two adjacent characters that form a bad pair and remove both of them. Continue this process until no more bad pairs exist.

Goal: Return the final great string after all possible removals. The result is guaranteed to be unique, and an empty string is considered great too!

Example: "leEeetcode" โ†’ "leetcode" (remove 'e' and 'E')

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "leEeetcode"
โ€บ Output: "leetcode"
๐Ÿ’ก Note: First, we remove 'eE' (positions 1,2) to get 'leetcode'. No more bad pairs exist, so we return 'leetcode'.
example_2.py โ€” Complete Elimination
$ Input: s = "abBAcC"
โ€บ Output: ""
๐Ÿ’ก Note: Remove 'bB' โ†’ 'aAcC', then remove 'aA' โ†’ 'cC', finally remove 'cC' โ†’ ''. The string becomes empty.
example_3.py โ€” No Bad Pairs
$ Input: s = "s"
โ€บ Output: "s"
๐Ÿ’ก Note: Single character string has no adjacent pairs, so it's already great. Return as is.

Constraints

  • 1 โ‰ค s.length โ‰ค 100
  • s contains only English letters (both lowercase and uppercase)
  • Adjacent characters must be checked for same letter different case

Visualization

Tap to expand
Smart Text Editor Auto-CorrectionText Editor - Auto Correct ModeTyping: leEeetcodeleBuffer: [l, e]Mistake Detected: 'E' conflicts with 'e'leEโš  Bad pair detected!Auto-Correction: Backspace removes both 'e' and 'E'lBuffer: [l] โœ“Continue Typing: Final ResultleetcodePerfect! No conflicts remaining โœ“Smart KeyboardโŒซAuto-correctโœ“Clean text
Understanding the Visualization
1
Type Characters
As you type, the editor maintains a buffer of 'good' characters
2
Detect Mistakes
When a bad pair is detected (like 'eE'), the editor automatically backspaces
3
Auto-Correct
The mistake is removed, and typing continues seamlessly
4
Clean Text
Final result is clean text with no case-conflict pairs
Key Takeaway
๐ŸŽฏ Key Insight: Use a stack to maintain 'good' characters - when a bad pair is detected, simply pop the conflicting character, just like hitting backspace!
Asked in
Facebook 15 Amazon 12 Google 8 Microsoft 6
125.0K Views
Medium Frequency
~12 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen