The Maximum Number of Words You Can Type coding problem gives you a text string (a sentence) and a brokenLetters string (containing all the characters that are broken on your keyboard). You need to count how many words in the text can be typed completely, meaning none of their characters appear in brokenLetters.
Quora, Microsoft, and Amazon use this as an easy problem to test fundamental string manipulation, set operations, and iteration skills. It's a good way to assess a candidate's ability to convert a problem into efficient data structure usage (like a hash set for brokenLetters).
Hash Set + Iteration: The most efficient way to check if a word contains any broken letters is to put all brokenLetters into a hash set for O(1) average-case lookup. Then, iterate through each word in the text. For each word, iterate through its characters. If any character is found in the brokenLetters hash set, the word cannot be typed; move to the next word. If a word is fully traversed without finding any broken letters, increment a counter.
broken_set (hash set) from brokenLetters for O(1) average-case lookups.typable_words_count = 0.text into individual words (e.g., using space as a delimiter).word in the words list:
a. Set can_type_word = true.
b. For each char in the word:
i. If char is in broken_set:
can_type_word = false.
Break from this inner loop (no need to check further characters in this word).
c. If can_type_word is true (after checking all its characters):
typable_words_count++.typable_words_count.text = "hello world", brokenLetters = "ad"
broken_set = {'a', 'd'}.
typable_words_count = 0.
Words: ["hello", "world"]
Word "hello":
broken_set.broken_set.broken_set.broken_set.broken_set.can_type_word is true. typable_words_count = 1.Word "world":
broken_set.broken_set.broken_set.broken_set.broken_set. can_type_word = false. Break.can_type_word is false, so typable_words_count remains 1.Result: 1.
brokenLetters for each character in each word results in O(L_text * L_word * L_broken) complexity, where L is length. Hash set reduces this to O(L_text * L_word + L_broken).For the Hash Table String interview pattern, this problem is a good example of how to use hash sets for efficient membership testing. It's a common technique for optimizing problems that involve checking for forbidden characters or elements.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Check if the Sentence Is Pangram | Easy | Solve | |
| Count the Number of Special Characters I | Easy | Solve | |
| Jewels and Stones | Easy | Solve | |
| Permutation Difference between Two Strings | Easy | Solve | |
| Second Largest Digit in a String | Easy | Solve |