There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly.
Given a string text
of words separated by a single space (no leading or trailing spaces) and a string brokenLetters
of all distinct letter keys that are broken, return the number of words in text
you can fully type using this keyboard.
Example 1:
Input: text = "hello world", brokenLetters = "ad" Output: 1 Explanation: We cannot type "world" because the 'd' key is broken.
Example 2:
Input: text = "leet code", brokenLetters = "lt" Output: 1 Explanation: We cannot type "leet" because the 'l' and 't' keys are broken.
Example 3:
Input: text = "leet code", brokenLetters = "e" Output: 0 Explanation: We cannot type either word because the 'e' key is broken.
Constraints:
1 <= text.length <= 104
0 <= brokenLetters.length <= 26
text
consists of words separated by a single space without any leading or trailing spaces.brokenLetters
consists of distinct 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 solution is like trying every single possible combination. We essentially try removing each letter, one by one, and see if that fixes the problem of typing too many words.
Here's how the algorithm would work step-by-step:
def max_number_of_words_you_can_type(text, broken_letters):
words = text.split()
typeable_words = []
# Iterate through each word in the given text
for word in words:
is_word_broken = False
# Iterate through each broken letter to check if the current word contains any
for broken_letter in broken_letters:
if broken_letter in word:
is_word_broken = True
break
# If the word does not contain any broken letters, add it to our typeable words list
if not is_word_broken:
typeable_words.append(word)
# Return the number of words that can be typed
return len(typeable_words)
The core idea is to quickly figure out which letters are broken and then count how many words we can completely type given those broken letters. Instead of individually checking each word against the broken letters, we can use a shortcut to efficiently count the valid words.
Here's how the algorithm would work step-by-step:
def maximum_number_of_words_you_can_type(text, broken_letters):
words = text.split()
number_of_typeable_words = 0
broken_letters_set = set(broken_letters)
for word in words:
# Check if current word contains any broken letters.
is_word_broken = False
for char in word:
if char in broken_letters_set:
is_word_broken = True
break
# Only increment if we can type the current word.
if not is_word_broken:
number_of_typeable_words += 1
return number_of_typeable_words
Case | How to Handle |
---|---|
text is null or empty | Return 0 since no words can be typed. |
brokenLetters is null or empty | Return the number of words in text, since all letters are typeable. |
text contains only whitespace | Return 0, as there are no valid words. |
brokenLetters contains all lowercase letters | Return 0 since no word can be typed |
text contains words with punctuation | Split the text by spaces and iterate, treating punctuation as part of the word, it will only fail if punctuation contains broken letters. |
text contains very long words | Ensure algorithm scales correctly, preventing performance issues in checking if the long word can be typed. |
text contains leading or trailing spaces | Trim the text to remove leading and trailing spaces before splitting. |
brokenLetters contains duplicate characters | The algorithm should treat this as only checking for the unique broken letters, not being affected by duplicates. |