Skip to content

Commit 6d332bb

Browse files
authored
Create 1935-maximum-number-of-words-you-can-type.java
1 parent d237e2a commit 6d332bb

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* LeetCode Problem: 1935. Maximum Number of Words You Can Type
3+
* https://leetcode.com/problems/maximum-number-of-words-you-can-type/
4+
*
5+
* Problem:
6+
* There is a string `text` consisting of words separated by a single space, and a string `brokenLetters` containing unique broken keyboard letters.
7+
* A word can be typed if none of its letters are in `brokenLetters`.
8+
* Return the number of words in `text` that can be typed.
9+
*
10+
* Example:
11+
* Input: text = "hello world", brokenLetters = "ad"
12+
* Output: 1
13+
*
14+
* Approach:
15+
* 1. Create a boolean array of size 26 to mark broken letters.
16+
* 2. For each broken letter, mark its corresponding index as true.
17+
* 3. Split the `text` into words by spaces.
18+
* 4. For each word, check every character:
19+
* - If any character is broken, skip counting this word.
20+
* - Otherwise, count it as a valid word.
21+
*
22+
* Time Complexity: O(n + m)
23+
* - n = length of `text`
24+
* - m = length of `brokenLetters`
25+
*
26+
* Space Complexity: O(1)
27+
* - Fixed array of size 26 is used for tracking broken letters.
28+
*/
29+
30+
public class Solution {
31+
public int canBeTypedWords(String text, String brokenLetters) {
32+
boolean[] charExists = new boolean[26];
33+
for (char c : brokenLetters.toCharArray()) {
34+
charExists[c - 'a'] = true;
35+
}
36+
37+
String[] textArr = text.split(" ");
38+
int count = 0;
39+
40+
for (String word : textArr) {
41+
boolean canType = true;
42+
for (int i = 0; i < word.length(); i++) {
43+
if (charExists[word.charAt(i) - 'a']) {
44+
canType = false;
45+
break;
46+
}
47+
}
48+
if (canType) {
49+
count++;
50+
}
51+
}
52+
53+
return count;
54+
}
55+
}

0 commit comments

Comments
 (0)