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.
You are given a string word, which represents the final output displayed on Alice's screen. You are also given a positive integer k.
Return the total number of possible original strings that Alice might have intended to type, if she was trying to type a string of size at least k.
Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: word = "aabbccdd", k = 7
Output: 5
Explanation:
The possible strings are: "aabbccdd", "aabbccd", "aabbcdd", "aabccdd", and "abbccdd".
Example 2:
Input: word = "aabbccdd", k = 8
Output: 1
Explanation:
The only possible string is "aabbccdd".
Example 3:
Input: word = "aaabbb", k = 3
Output: 8
Constraints:
1 <= word.length <= 5 * 105word consists only of lowercase English letters.1 <= k <= 2000When 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 core idea is to try out all possible ways the shorter string could have been created from the longer string. We will explore every combination by checking if removing characters from the longer string could result in the shorter string.
Here's how the algorithm would work step-by-step:
def find_original_typed_string_brute_force(original_string, typed_string):
def solve(original_index, typed_index, current_string):
if original_index == len(original_string) and typed_index == len(typed_string):
return True
if original_index < len(original_string) and typed_index == len(typed_string):
return False
if original_index == len(original_string) and typed_index < len(typed_string):
# We can only remove characters from typed_string at this point.
return True
# Option 1: Keep the character in typed_string
if original_string[original_index] == typed_string[typed_index]:
if solve(original_index + 1, typed_index + 1, current_string + typed_string[typed_index]):
return True
# Option 2: Remove the character in typed_string
# We are skipping the current char
if solve(original_index, typed_index + 1, current_string):
return True
return False
if solve(0, 0, ""):
return True
else:
return FalseThis problem requires us to identify the original string given a potentially longer, typed string which might include duplications of characters. The most efficient strategy uses a technique to carefully compare the potential original string with the typed string, using a process that recognizes and skips over duplicated characters to determine the actual original string.
Here's how the algorithm would work step-by-step:
def find_original_string(original_string, typed_string):
original_index = 0
typed_index = 0
while typed_index < len(typed_string):
if original_index < len(original_string) and \
original_string[original_index] == typed_string[typed_index]:
original_index += 1
typed_index += 1
else:
# Check for repeated characters (typos)
if typed_index > 0 and \
typed_string[typed_index] == typed_string[typed_index - 1]:
# Skip the repeated character in typed_string
typed_index += 1
else:
# Characters don't match, not a repetition, not in original
return False
# Ensure we've reached the end of the original string
if original_index != len(original_string):
return False
return True| Case | How to Handle |
|---|---|
| s is null or empty | Return an empty string since there's nothing to derive from. |
| t is null or empty | Return s as the solution since anything in s that's extra can simply be removed when t is nothing. |
| s is shorter than t | Return an empty string or null, as s cannot contain all characters of t with added repetitions. |
| s and t are identical | Return s (or t) directly, since no characters need to be removed. |
| t is a substring of s, but with extra characters in s | Iterate through s and t, removing extra repeated characters in s while maintaining the order of characters in t. |
| Characters in t are not in s | Return an empty string or null, as s does not contain all the characters from t. |
| s consists of repeating characters, but t does not | Iterate through s and only keep characters when it matches the corresponding position in t, otherwise remove repeating characters in s. |
| Consecutive repeating characters in 's' are more than what's allowable based on 't' | When s[i] matches t[j] check consecutive occurences of s[i] and t[j] to ensure the number of occurrences in 's' is not exceeding allowable count in 't' + 1. For example, if t[j] repeats 'k' times, then s[i] can at most repeat 'k+1' times consecutively. |