EasyNo. 1935
Maximum Number of Words You Can Type — LeetCode 1935 Python Solution
- Pattern
- Hash Map
- Topics
- Hash Table, String
- Reading time
- 2 min
- Source
- leetcode.com
The problem
There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly.
Example
- Input
- text = "hello world", brokenLetters = "ad"
- Output
- 1
- Explanation
- We cannot type "world" because the 'd' key is broken.
Python solution
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
s = set(brokenLetters)
return sum(all(c not in s for c in w) for w in text.split())Complexity
- Time
- O(n)
- Space · auxiliary
- O(|Σ|), where n is the length of the string text, and |Σ| is the size of the alphabet
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 1935. Maximum Number of Words You Can Type is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1930 | Unique Length-3 Palindromic Subsequences | Medium |
| 1941 | Check if All Characters Have Equal Number of Occurrences | Easy |
| 0003 | Longest Substring Without Repeating Characters | Medium |
| 0012 | Integer to Roman | Medium |
| 0013 | Roman to Integer | Easy |
| 0017 | Letter Combinations of a Phone Number | Medium |
Frequently asked questions
- How hard is LeetCode 1935. Maximum Number of Words You Can Type?
LeetCode 1935. Maximum Number of Words You Can Type is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1935. Maximum Number of Words You Can Type?
The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 1935. Maximum Number of Words You Can Type?
The Python solution on this page uses O(|Σ|), where n is the length of the string text, and |\Sigma| is the size of the alphabet auxiliary space.
- What topics does LeetCode 1935. Maximum Number of Words You Can Type cover?
LeetCode 1935. Maximum Number of Words You Can Type is tagged Hash Table and String on LeetCode.