Make The String Great - Complete Solution Guide
Make The String Great is LeetCode problem 1544, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks you to clean up a string by removing adjacent characters that are the same letter but in different cases (e.g., 'l' and 'L'). The input is a string containing uppercase and lowercase English letters. The output is the 'good' string resulting after removing all such adjacent pairs. A 'good' string is defined as a string where no two adjacent characters are the same letter in different cases. The process of removing these pairs can be repeated until no such pairs remain. The solution is guaranteed to be unique.
Solution Approach
The provided solutions employ a stack-based approach. The algorithm iterates through the input string. If the current character and the top of the stack form a 'bad' pair (same letter, different case), the pair is removed by popping the stack. Otherwise, the current character is pushed onto the stack. Finally, the stack is converted back into a string, which is the 'good' string.
Step-by-Step Algorithm
- Initialize an empty stack.
- Iterate through each character in the input string.
- Check if the stack is not empty and if the absolute difference between the ASCII values of the current character and the top of the stack is 32 (indicating a 'bad' pair).
- If it's a 'bad' pair, pop the top element from the stack.
- Otherwise, push the current character onto the stack.
- After iterating through the entire string, the stack contains the characters of the 'good' string.
- Convert the stack into a string and return it.
Key Insights
- Using a stack is a natural choice for this problem because it allows easy removal of adjacent pairs. When we encounter a character that cancels out the top of the stack, we pop the stack. Otherwise, we push the character onto the stack.
- The difference between the ASCII values of a lowercase and uppercase letter is exactly 32. This property can be efficiently used to check if two characters are the same letter in different cases.
- The problem can be solved iteratively by repeatedly scanning the string and removing bad pairs until no more exist; however, the stack-based approach is more efficient.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: String, Stack.
Companies
Asked at: Accenture, BlackStone.