Maximum Number of Words You Can Type (LeetCode 1935): Solution and Explanation

Easy
Hash Table
String

Some letter keys on a keyboard are broken. Given a sentence and the set of broken letters, count how many words of the sentence can be typed in full.

Intuition

A word can be typed only if every key it needs works. One broken letter anywhere in the word is enough to rule it out.

So each word is a yes or no question about its letters, and looking a letter up in a set of broken keys answers it in constant time. Stop reading a word at its first broken letter.

Approach: Broken-key set and a word scan

  1. Put the letters of brokenLetters into a set (or a 26-slot boolean array).
  2. Split text on spaces.
  3. For each word, scan its letters. If any letter is in the broken set, the word fails.
  4. Count the words that never hit a broken letter and return the count.

Step-by-step example

text = "hello world", brokenLetters = "ad"
  1. The broken set is {a, d}.
  2. "hello" uses h, e, l, l, o. None is broken, so count = 1.
  3. "world" uses w, o, r, l, then d. The 'd' key is broken, so the word is skipped.
  4. The answer is 1.

Code: Python, Java, C++

1class Solution:
2    def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
3        broken = set(brokenLetters)
4        count = 0
5        for word in text.split():
6            if not any(c in broken for c in word):
7                count += 1
8        return count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n + m)

Every character of the text is read at most once, and building the set reads the m broken letters once.

Space

O(1)

The broken set holds at most 26 letters. Splitting the text into words uses O(n) if you count that copy.

Other approaches

Scan the text without splitting (time O(n + m), space O(1))

Walk the text once with a flag that records whether the current word has hit a broken key. Add one at each space or at the end when the flag is clear. This avoids building the list of words.

Check each broken letter against each word (time O(26 n), space O(1))

For every word, test whether it contains each of the broken letters. With up to 26 broken letters this is a constant factor slower but still linear.

Common mistakes

  • Counting the broken words instead of the typeable ones.
  • Forgetting that brokenLetters can be empty, in which case every word counts.
  • Splitting on a regex or trimming when the input already guarantees single spaces, and getting an empty word from an off-by-one manual split.

Frequently asked questions

What if no key is broken?

Then every word can be typed and the answer is the number of words.

Why use a set rather than searching the brokenLetters string?

Searching the string costs up to 26 steps per letter. A set or boolean array answers each lookup in one step, though both are linear overall.

Can I stop early inside a word?

Yes. As soon as one letter is broken the word is lost, so the rest of its letters do not need checking.