We define the string base to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so base will look like this:
"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".Given a string s, return the number of unique non-empty substrings of s are present in base.
Example 1:
Input: s = "a" Output: 1 Explanation: Only the substring "a" of s is in base.
Example 2:
Input: s = "cac"
Output: 2
Explanation: There are two substrings ("a", "c") of s in base.
Example 3:
Input: s = "zab"
Output: 6
Explanation: There are six substrings ("z", "a", "b", "za", "ab", and "zab") of s in base.
Constraints:
1 <= s.length <= 105s consists of lowercase 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 brute force approach to finding unique substrings in a repeating alphabetical sequence involves generating every single possible piece of the sequence. Then, we check each piece to see if it follows the continuous alphabetical order and keep track of the unique ones we have seen.
Here's how the algorithm would work step-by-step:
def findSubstringInWraproundString(input_string):
unique_valid_pieces = set()
for start_index in range(len(input_string)):
for end_index in range(start_index + 1, len(input_string) + 1):
current_piece = input_string[start_index:end_index]
# We assume the piece is valid until proven otherwise by a broken alphabetical sequence
is_alphabetical_order = True
for character_index in range(len(current_piece) - 1):
current_char_code = ord(current_piece[character_index])
next_char_code = ord(current_piece[character_index + 1])
if next_char_code == current_char_code + 1 or (current_char_code == 122 and next_char_code == 97):
continue
else:
is_alphabetical_order = False
break
# Only consider pieces that follow the continuous wrapround alphabetical order rule
if is_alphabetical_order:
unique_valid_pieces.add(current_piece)
else:
continue
# Counting all accumulated unique pieces gives the final required answer
return len(unique_valid_pieces)
The problem asks for the number of unique non-empty substrings of a given string that also appear in an infinite wraparound alphabet string. Any valid substring in the wraparound string that ends at a specific character is uniquely determined by its length and its ending character. Therefore, by tracking the maximum length of contiguous wraparound sequences ending at each of the 26 lowercase English letters, we can avoid double counting and simply sum these maximum lengths.
Here's how the algorithm would work step-by-step:
def find_substring_in_wrapround_string(text_string):
# Track the maximum length of contiguous valid substrings ending at each character
max_length_ending_at = {}
current_valid_length = 0
# Iterate through the characters to find lengths of contiguous sequences
for index, current_char in enumerate(text_string):
if index > 0 and (ord(current_char) - ord(text_string[index - 1])) % 26 == 1:
current_valid_length += 1
else:
current_valid_length = 1
# Update the maximum valid length ending with current_char
previous_max = max_length_ending_at.get(current_char, 0)
if current_valid_length > previous_max:
max_length_ending_at[current_char] = current_valid_length
# The total number of unique substrings is the sum of maximum lengths for each ending character
return sum(max_length_ending_at.values())
| Case | How to Handle |
|---|---|
| Empty string input | Return zero immediately as there are no non-empty substrings to evaluate. |
| Single character string | Return one since any single character is inherently part of the alphabet wraparound string. |
| String with no consecutive alphabetical wraparounds | Calculate the sum of individual unique characters by tracking a streak length of one for each. |
| String with repeating identical characters | Keep only the maximum consecutive length ending at each character to avoid counting duplicate substrings. |
| Boundary wraparound from z to a | Check adjacency by ensuring the current character ASCII code is exactly one greater than the previous or wraps from z to a. |
| Long overlapping sequences of the same substring | Store the maximum streak length seen so far for each ending character in an array or hash map. |
| Maximum-sized input string up to constraints | Iterate through the string in linear time using a fixed-size frequency array for characters to scale efficiently. |