Skip to main content
Back to problems
Leetcode
Medium
Stacks
Strings
Make The String Great

Repeatedly remove adjacent letters that are the same alphabetic character but opposite in case, until the string becomes stable.

Acceptance 0%
Problem Statement

You are given a string ss consisting only of English letters.

A string is considered good if it does not contain any adjacent pair of characters where:

  • the two characters are the same letter, and
  • one is lowercase while the other is uppercase.

In one move, you may delete any such adjacent bad pair from the string.

Keep performing deletions until no more bad pairs remain. Return the final string after all possible removals.

The order of the remaining characters must stay the same as in the original string.

Input Format

  • A single string ss consisting of English letters only.

Output Format

  • Return the string obtained after repeatedly removing all adjacent bad pairs until the string is good.

Constraints

  • 1s1051 \le |s| \le 10^5
  • ss contains only uppercase and lowercase English letters
  • The answer is unique regardless of deletion order
Examples
Sample cases returned by the problem API.

Example 1

Input

"leEeetcode"

Output

"leetcode"

Explanation

The substring "eE" cancels out, leaving "leetcode".

Example 2

Input

"abBAcC"

Output

""

Explanation

Each adjacent opposite-case pair can be removed until nothing remains.

Show 1 more example

Example 3

Input

"s"

Output

"s"

Explanation

A single character is already good.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.