Skip to content

Commit 8d24724

Browse files
chore: add LeetCode daily solution
1 parent 98f0c13 commit 8d24724

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Maximum Number of Words You Can Type (Easy)
2+
3+
**Problem ID:** 1935
4+
**Date:** 2025-09-15
5+
**Link:** https://leetcode.com/problems/maximum-number-of-words-you-can-type/
6+
7+
## Approach
8+
9+
To solve the problem of determining the maximum number of words that can be fully typed with a malfunctioning keyboard, we can follow a straightforward approach:
10+
11+
### Approach:
12+
13+
1. **Input Parsing**: Split the input string `text` into individual words using space as a delimiter. This can be easily achieved using the built-in string split function.
14+
15+
2. **Broken Letters Representation**: Convert the string `brokenLetters` into a set for efficient membership checking. This allows us to quickly determine if any character in a word is broken.
16+
17+
3. **Word Validation**: For each word in the list of words obtained from `text`, check if it can be fully typed. This involves iterating through each character of the word and checking if it exists in the set of broken letters. If a character is found in the set, the word cannot be typed.
18+
19+
4. **Counting Valid Words**: Maintain a counter to keep track of the number of words that can be fully typed. Increment this counter for each word that passes the validation check.
20+
21+
5. **Output the Result**: Finally, return the count of valid words.
22+
23+
### Main Idea:
24+
The key idea is to leverage the efficiency of set lookups to quickly determine if any characters in a word are broken. This allows us to validate each word in linear time relative to its length.
25+
26+
### Data Structures:
27+
- **List**: To store the words from the input string `text`.
28+
- **Set**: To store the broken letters for O(1) average time complexity on membership tests.
29+
30+
### Complexity:
31+
- **Time Complexity**: O(n * m), where n is the number of words in `text` and m is the average length of the words. This is because we check each character of each word against the set of broken letters.
32+
- **Space Complexity**: O(k), where k is the number of distinct broken letters (at most 26), for storing the set of broken letters.
33+
34+
This approach is efficient given the constraints and effectively counts the number of words that can be typed with the malfunctioning keyboard.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int canBeTypedWords(String text, String brokenLetters) {
3+
Set<Character> brokenSet = new HashSet<>();
4+
for (char c : brokenLetters.toCharArray()) {
5+
brokenSet.add(c);
6+
}
7+
8+
String[] words = text.split(" ");
9+
int count = 0;
10+
11+
for (String word : words) {
12+
boolean canType = true;
13+
for (char c : word.toCharArray()) {
14+
if (brokenSet.contains(c)) {
15+
canType = false;
16+
break;
17+
}
18+
}
19+
if (canType) {
20+
count++;
21+
}
22+
}
23+
24+
return count;
25+
}
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var canBeTypedWords = function(text, brokenLetters) {
2+
const brokenSet = new Set(brokenLetters);
3+
const words = text.split(' ');
4+
let count = 0;
5+
6+
for (const word of words) {
7+
if (![...word].some(char => brokenSet.has(char))) {
8+
count++;
9+
}
10+
}
11+
12+
return count;
13+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
3+
broken_set = set(brokenLetters)
4+
words = text.split()
5+
count = 0
6+
7+
for word in words:
8+
if not any(char in broken_set for char in word):
9+
count += 1
10+
11+
return count

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,4 @@ Each problem includes:
148148
- 2025-09-12 — [Vowels Game in a String](https://leetcode.com/problems/vowels-game-in-a-string/) (Medium) → `Medium/2025-09-12-3227-Vowels-Game-in-a-String`
149149
- 2025-09-13 — [Find Most Frequent Vowel and Consonant](https://leetcode.com/problems/find-most-frequent-vowel-and-consonant/) (Easy) → `Easy/2025-09-13-3541-Find-Most-Frequent-Vowel-and-Consonant`
150150
- 2025-09-14 — [Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/) (Medium) → `Medium/2025-09-14-966-Vowel-Spellchecker`
151+
- 2025-09-15 — [Maximum Number of Words You Can Type](https://leetcode.com/problems/maximum-number-of-words-you-can-type/) (Easy) → `Easy/2025-09-15-1935-Maximum-Number-of-Words-You-Can-Type`

0 commit comments

Comments
 (0)