Maximum Number of Words You Can Type - Problem
Imagine you're working on an old computer with a faulty keyboard where some keys just won't respond when you press them! Your task is to determine how many complete words from a given text you can actually type.
You're given:
- A
textstring containing words separated by single spaces (no leading or trailing spaces) - A
brokenLettersstring containing all the distinct letter keys that don't work
Your goal is to return the number of words in the text that you can fully type using this malfunctioning keyboard.
Example: If your text is "hello world" and the broken letters are "ad", you can type "hello" completely (no 'a' or 'd' needed), but you cannot type "world" because it contains the broken letter 'd'. So the answer would be 1.
Input & Output
example_1.py โ Basic Case
$
Input:
text = "hello world", brokenLetters = "ad"
โบ
Output:
1
๐ก Note:
We can type "hello" completely since it doesn't contain 'a' or 'd'. However, "world" contains 'd' which is broken, so we can't type it. Total: 1 word.
example_2.py โ Multiple Broken Letters
$
Input:
text = "leet code", brokenLetters = "lt"
โบ
Output:
1
๐ก Note:
"leet" contains both 'l' and 't' which are broken, so it cannot be typed. "code" doesn't contain 'l' or 't', so it can be typed. Total: 1 word.
example_3.py โ No Broken Letters
$
Input:
text = "leet code", brokenLetters = ""
โบ
Output:
2
๐ก Note:
No letters are broken, so we can type all words: "leet" and "code". Total: 2 words.
Constraints
- 1 โค text.length โค 104
- 0 โค brokenLetters.length โค 26
- text consists of words separated by single spaces, no leading or trailing spaces
- Each word consists only of lowercase English letters
- brokenLetters consists of distinct lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Build Broken Keys Set
Create a fast lookup table of all the broken keyboard keys
2
Examine Each Word
Go through each word in the text one by one
3
Check Every Letter
For each word, verify that none of its letters are in the broken keys set
4
Count Typeable Words
Only count words that can be completely typed without any broken letters
Key Takeaway
๐ฏ Key Insight: Use a hash set to convert the broken letters into a fast O(1) lookup table, then check each word's letters against this set. This transforms an O(nรmรk) brute force solution into an optimal O(n+m) solution.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code