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 - 2s[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.
Example 1:
Input: s = "leEeetcode" Output: "leetcode" Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".
Example 2:
Input: s = "abBAcC" Output: "" Explanation: We have many possible scenarios, and all lead to the same answer. For example: "abBAcC" --> "aAcC" --> "cC" --> "" "abBAcC" --> "abBA" --> "aA" --> ""
Example 3:
Input: s = "s" Output: "s"
Constraints:
1 <= s.length <= 100s contains only lower and upper case English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to clean up a string by repeatedly removing pairs of characters that are the same letter but different cases (like 'a' and 'A'). The brute force method involves checking and removing these pairs over and over until no more pairs can be removed.
Here's how the algorithm would work step-by-step:
def make_the_string_great_brute_force(input_string):
string_changed = True
while string_changed:
string_changed = False
new_string = ""
index = 0
while index < len(input_string):
# Check for adjacent characters that need removal
if index + 1 < len(input_string) and \
input_string[index].lower() == input_string[index + 1].lower() and \
input_string[index] != input_string[index + 1]:
# A pair was found, so skip both characters
index += 2
string_changed = True
else:
# Keep the current character if no match is found
new_string += input_string[index]
index += 1
# Update the string for the next iteration
input_string = new_string
return input_stringThe goal is to remove adjacent letter pairs from a string if they are the same letter but different cases (one uppercase, one lowercase). The key is to keep track of the characters we've seen so far and quickly check for these bad pairs as we go.
Here's how the algorithm would work step-by-step:
def make_the_string_great(input_string):
character_stack = []
for current_character in input_string:
# Check if the stack is not empty
if character_stack:
top_of_stack = character_stack[-1]
# Check if the current character forms a bad pair with the top of the stack
if (current_character.islower() and top_of_stack.isupper() and current_character.upper() == top_of_stack) or \
(current_character.isupper() and top_of_stack.islower() and current_character.lower() == top_of_stack):
# Remove the top character from the stack
character_stack.pop()
else:
# Add the current character to the stack
character_stack.append(current_character)
else:
# Add the current character to the stack if stack is empty
character_stack.append(current_character)
# Construct the great string from the remaining characters in the stack
return "".join(character_stack)| Case | How to Handle |
|---|---|
| Empty input string | Return an empty string immediately as there's nothing to process. |
| String with a single character | Return the single-character string as it cannot be 'made bad'. |
| String with two characters that cancel each other out | Return an empty string as the two characters negate each other. |
| String with no 'bad' character pairs | Return the original string, as no removals are necessary. |
| String with maximum allowed length | Ensure the solution's time and space complexity allow for processing without exceeding memory or time limits. |
| String where all characters are the same case (all uppercase or all lowercase) | The algorithm should recognize there are no cancelling pairs and return the original string. |
| Alternating pairs of bad characters resulting in continuous cancellations | The stack-based approach correctly handles iterative cancellations. |
| Very long string with many cancelling pairs close to the beginning, requiring repeated removals | The solution should efficiently handle a large number of removals, preventing performance degradation. |