Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and may press a key for too long, resulting in a character being typed multiple times.
Although Alice tried to focus on her typing, she is aware that she may still have done this at most once.
You are given a string word, which represents the final output displayed on Alice's screen.
Return the total number of possible original strings that Alice might have intended to type.
Example 1:
Input: word = "abbcccc"
Output: 5
Explanation:
The possible strings are: "abbcccc", "abbccc", "abbcc", "abbc", and "abcccc".
Example 2:
Input: word = "abcd"
Output: 1
Explanation:
The only possible string is "abcd".
Example 3:
Input: word = "aaaa"
Output: 4
Constraints:
1 <= word.length <= 100word consists only 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 basic idea is to try out all possible ways to build the original string. We'll check each possibility to see if it matches the longer, typed-out string.
Here's how the algorithm would work step-by-step:
def find_original_string_brute_force(typed_string):
for original_length in range(1, len(typed_string) + 1):
possible_original = typed_string[:original_length]
# Check if this possible original string can produce the typed string
if can_form_typed_string(possible_original, typed_string):
return possible_original
return ""
def can_form_typed_string(possible_original, typed_string):
original_index = 0
typed_index = 0
while original_index < len(possible_original) and typed_index < len(typed_string):
if possible_original[original_index] == typed_string[typed_index]:
original_index += 1
typed_index += 1
elif typed_index > 0 and typed_string[typed_index] == typed_string[typed_index - 1]:
# Allow for repeated characters in typed string.
typed_index += 1
else:
return False
# The original string should be fully traversed to form the typed string.
if original_index != len(possible_original):
return False
#Remaining typed characters must match the last original character.
while typed_index < len(typed_string):
if typed_string[typed_index] != possible_original[original_index - 1]:
return False
typed_index += 1
return TrueWe're given a longer string and a shorter string, where the longer string contains the original message with some extra characters added in. The goal is to figure out what the original message was by carefully comparing the two strings, character by character.
Here's how the algorithm would work step-by-step:
def find_original_string(long_string, short_string):
long_string_index = 0
short_string_index = 0
original_message = ''
while long_string_index < len(long_string):
# Characters match, add to result and advance both pointers.
if short_string_index < len(short_string) and \
long_string[long_string_index] == short_string[short_string_index]:
original_message += long_string[long_string_index]
long_string_index += 1
short_string_index += 1
else:
# Characters don't match, advance only the long string pointer.
# This skips the extra characters.
long_string_index += 1
return original_message| Case | How to Handle |
|---|---|
| s is null or empty | Return an empty string if s is null or empty as there's no typed string. |
| t is null or empty | Return s if t is null or empty, since an empty displayed string means all characters in s were potentially accidental. |
| Length of s is less than length of t | Return an empty string since t can't be a subsequence of s if it's longer. |
| s and t are identical | Return s (or t) since the original string is the same as the final displayed string. |
| t is not a subsequence of s | Return an empty string because no valid original string can be derived. |
| s contains duplicate characters and t utilizes specific occurrences | The subsequence matching should respect the order of characters in both s and t. |
| Maximum length strings for s and t exceeding memory limits | The solution should use an iterative approach rather than recursion to avoid stack overflow, and should consider memory usage when constructing the result string. |
| s contains only one character and t is empty | Return s since the character in s was removed to create the empty t. |