Make The String Great - Problem

Given a string s of lower and upper case English letters.

A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where:

  • 0 <= i <= s.length - 2
  • s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.

To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.

Return the string after making it good. The answer is guaranteed to be unique under the given constraints.

Notice that an empty string is also good.

Input & Output

Example 1 — Basic Removal
$ Input: s = "leEeetcode"
Output: "leetcode"
💡 Note: Remove the 'eE' pair: 'e' and 'E' are the same letter but different cases, so we remove both. Result: "leetcode"
Example 2 — Multiple Removals
$ Input: s = "abBAcC"
Output: ""
💡 Note: Remove 'bB' → "aAcC", then remove 'cC' → "aA", finally remove 'aA' → "". Result: empty string
Example 3 — No Removals Needed
$ Input: s = "s"
Output: "s"
💡 Note: Single character has no adjacent pairs to remove. Result: "s"

Constraints

  • 1 ≤ s.length ≤ 100
  • s contains only lowercase and uppercase English letters

Visualization

Tap to expand
Make The String Great: "leEeetcode" → "leetcode"Input StringleEeetcoBad Pair: e ≠ E but same letter → Remove both!Good String (Result)leetcode"leetcode" - No adjacent same-letter different-case pairs!
Understanding the Visualization
1
Input String
String with potential bad pairs (same letter, different cases)
2
Remove Bad Pairs
Eliminate adjacent characters that are same letter but different cases
3
Good String
Final string with no bad pairs remaining
Key Takeaway
🎯 Key Insight: Use a stack to efficiently handle cascading removals when characters cancel each other out
Asked in
Facebook 15 Amazon 12
94.0K Views
Medium Frequency
~15 min Avg. Time
1.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