Skip to content

Commit e9efa51

Browse files
committedDec 23, 2021
solves maximum number of words you can type
1 parent ee92b83 commit e9efa51

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
 

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@
465465
| 1925 | [Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples) | [![Java](assets/java.png)](src/CountSquareSumTriplets.java) | |
466466
| 1929 | [Concatenation of Array](https://leetcode.com/problems/concatenation-of-array) | [![Java](assets/java.png)](src/ConcatenationOfArray.java) | |
467467
| 1933 | 🔒 [Check If String Is Decomposable Into Value EqualSubstrings](https://leetcode.com/problems/check-if-string-is-decomposable-into-value-equal-substring) | | |
468-
| 1935 | [Maximum Number of Words You Can Type](https://leetcode.com/problems/maximum-number-of-words-you-can-type) | | |
468+
| 1935 | [Maximum Number of Words You Can Type](https://leetcode.com/problems/maximum-number-of-words-you-can-type) | [![Java](assets/java.png)](src/MaximumNumberOfWordsYouCanType.java) | |
469469
| 1941 | [Check if All Characters Have Equal Number of Occurrences](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences) | | |
470470
| 1945 | [Sum of Digits of String After Convert](https://leetcode.com/problems/sum-of-digits-of-string-after-convert) | | |
471471
| 1952 | [Three Divisors](https://leetcode.com/problems/three-divisors) | | |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/maximum-number-of-words-you-can-type
2+
// T: O(|brokenLetters| + |text|)
3+
// S: O(1)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class MaximumNumberOfWordsYouCanType {
9+
public int canBeTypedWords(String text, String brokenLetters) {
10+
final Set<Character> brokenCharacters = getCharacters(brokenLetters);
11+
int words = 0;
12+
boolean canType = true;
13+
for (int i = 0 ; i < text.length() ; i++) {
14+
if (text.charAt(i) == ' ') {
15+
if (canType) words++;
16+
canType = true;
17+
} else if (brokenCharacters.contains(text.charAt(i))) {
18+
canType = false;
19+
}
20+
}
21+
return words + (canType ? 1 : 0);
22+
}
23+
24+
private Set<Character> getCharacters(String s) {
25+
final Set<Character> set = new HashSet<>();
26+
for (int i = 0 ; i < s.length() ; i++) {
27+
set.add(s.charAt(i));
28+
}
29+
return set;
30+
}
31+
}

0 commit comments

Comments
 (0)
Please sign in to comment.