A fancy string is a string where no three consecutive characters are equal.
Given a string s
, delete the minimum possible number of characters from s
to make it fancy.
Return the final string after the deletion. It can be shown that the answer will always be unique.
For example:
s = "leeetcode"
should return "leetcode"
s = "aaabaaa"
should return "aabaa"
s = "aab"
should return "aab"
Write a function that takes a string as input and returns a fancy string.
What is the time and space complexity of your solution?
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 brute force way to solve this is to try removing every possible combination of characters from the string. Then, for each of those combinations, we check if the resulting string is fancy according to the rules.
Here's how the algorithm would work step-by-step:
def delete_characters_to_make_fancy_string_brute_force(input_string):
longest_fancy_string = ""
string_length = len(input_string)
# Iterate through all possible combinations of characters to remove
for i in range(2 ** string_length):
current_string = ""
for j in range(string_length):
# If the j-th bit of i is not set, keep the character
if (i >> j) & 1 == 0:
current_string += input_string[j]
#Check if the resulting string is a fancy string
if is_fancy(current_string):
# Update the longest fancy string if the current one is longer
if len(current_string) > len(longest_fancy_string):
longest_fancy_string = current_string
return longest_fancy_string
def is_fancy(test_string):
if not test_string:
return True
count = 1
for i in range(1, len(test_string)):
# Reset count if characters are different
if test_string[i] != test_string[i-1]:
count = 1
else:
count += 1
# Not fancy if 3 or more consecutive chars are the same
if count >= 3:
return False
return True
The key idea is to go through the string once and remove extra characters to prevent having more than two of the same characters in a row. We build our fancy string character by character, making decisions based on the previous two characters.
Here's how the algorithm would work step-by-step:
def make_fancy_string(input_string):
fancy_string = ""
for char in input_string:
# Prevent more than two repeating chars
if len(fancy_string) >= 2 and \
char == fancy_string[-1] and \
char == fancy_string[-2]:
continue
fancy_string += char
return fancy_string
Case | How to Handle |
---|---|
Empty string | Return the empty string directly as it's already a fancy string. |
String with one or two characters | Return the original string as these cases are inherently fancy strings. |
String with three or more identical characters | Iterate and remove the extra characters exceeding the consecutive limit of two. |
String with alternating characters (e.g., 'ababab') | The algorithm should leave these strings untouched, as they are already fancy. |
String with long sequences of the same character (e.g., 'aaaaabbbb') | The algorithm must correctly reduce each sequence to a maximum of two repeating characters. |
Maximum length string consisting of repeating characters | Ensure the algorithm scales efficiently to handle large strings without excessive time complexity. |
String with mixed sequences of repeating and alternating characters. | The algorithm must handle the repeating and alternating character conditions simultaneously. |
String consisting of only 2 repeating characters | Return the input string as no removal is needed. |