You are given a string s
consisting of digits. An awesome substring is a non-empty substring of s
such that we can make any number of swaps in order to make it a palindrome. Return the length of the maximum length awesome substring of s
.
For example:
s = "3242415"
. The output is 5 because "24241"
is the longest awesome substring, and we can form the palindrome "24142"
with some swaps.s = "12345678"
. The output is 1, since no substring has all even counts except single digits.s = "213123"
. The output is 6 because "213123"
is the longest awesome substring, and we can form the palindrome "231132"
with some swaps.Write an algorithm to efficiently find the length of the maximum length awesome substring.
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 approach to this problem is all about trying every possible substring within the given string. We check each substring to see if it meets the 'awesome' criteria, and then keep track of the longest one we've found so far.
Here's how the algorithm would work step-by-step:
def find_longest_awesome_substring_brute_force(input_string):
longest_awesome_substring = ""
for substring_length in range(1, len(input_string) + 1):
for start_index in range(len(input_string) - substring_length + 1):
end_index = start_index + substring_length
current_substring = input_string[start_index:end_index]
# The substring has to be checked for being awesome
if is_awesome_substring(current_substring):
# We must update if current is larger
if len(current_substring) > len(longest_awesome_substring):
longest_awesome_substring = current_substring
return longest_awesome_substring
def is_awesome_substring(substring):
digit_counts = [0] * 10
for character in substring:
digit = int(character)
digit_counts[digit] += 1
odd_count = 0
for count in digit_counts:
if count % 2 != 0:
odd_count += 1
# An awesome string can have at most one digit with an odd count.
return odd_count <= 1
The key to finding the longest awesome substring efficiently lies in understanding that an awesome substring has at most one digit appearing an odd number of times. We use a clever trick to keep track of which digits appear an odd number of times so far and quickly check if a substring is awesome.
Here's how the algorithm would work step-by-step:
def find_longest_awesome_substring(input_string):
string_length = len(input_string)
first_occurrence = {0: -1}
current_mask = 0
max_length = 0
# We iterate to generate bitmasks
for index in range(string_length):
digit = int(input_string[index])
current_mask ^= (1 << digit)
# If a mask has been seen before, update the max length
if current_mask in first_occurrence:
max_length = max(max_length, index - first_occurrence[current_mask])
else:
first_occurrence[current_mask] = index
# Check all possible one-digit flips
for digit_to_flip in range(10):
flipped_mask = current_mask ^ (1 << digit_to_flip)
if flipped_mask in first_occurrence:
max_length = max(max_length, index - first_occurrence[flipped_mask])
return max_length
Case | How to Handle |
---|---|
Null or empty input string | Return 0, as there's no substring to evaluate. |
Input string with only one character | Return 1, as a single character is trivially an awesome substring. |
Input string with maximum allowed length (scalability) | The bitmask approach with prefix XOR handles large inputs efficiently due to its O(N) time complexity, avoiding timeouts. |
Input string containing all identical characters | The algorithm correctly identifies awesome substrings even with repeated characters, as it depends on odd/even counts. |
Input string where no awesome substring exists | The algorithm correctly calculates distances between odd-count states and returns 0 if no awesome substring exists, which happens when no two prefixes have same bitmask. |
Input with multiple longest awesome substrings | The algorithm finds the *longest* awesome substring, not necessarily all of them, but correctly returns the length of one longest substring. |
Integer overflow in length calculations (language-specific) | Since the problem states maximum string length of 10^5, integer overflow is not a concern when calculating length differences. |
Input string contains non-numeric characters | The problem states input string consists of digits, so non-numeric characters are not considered, and solution directly casts each character to integer. |