Skip to content

Commit 16c2825

Browse files
solves count the number of consistent strings
1 parent 3ca4225 commit 16c2825

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@
414414
| 1668 | [Maximum Repeating Substring](https://leetcode.com/problems/maximum-repeating-substring) | [![Java](assets/java.png)](src/MaximumRepeatingSubString.java) | |
415415
| 1672 | [Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth) | [![Java](assets/java.png)](src/RichestCustomerWealth.java) | |
416416
| 1678 | [Goal Parser Interpretation](https://leetcode.com/problems/goal-parser-interpretation) | [![Java](assets/java.png)](src/GoalParserInterpretation.java) | |
417-
| 1684 | [Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings) | | |
417+
| 1684 | [Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings) | [![Java](assets/java.png)](src/CountTheNumberOfConsistentStrings.java) | |
418418
| 1688 | [Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament) | | |
419419
| 1694 | [Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number) | | |
420420
| 1700 | [Number of Students Unable to Eat Lunch](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch) | | |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// T: O(allowed + words * word)
2+
// S: O(allowed)
3+
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class CountTheNumberOfConsistentStrings {
8+
public int countConsistentStrings(String allowed, String[] words) {
9+
final Set<Character> allowedCharacters = getCharacters(allowed);
10+
int consistentStrings = 0;
11+
for (String word :words) {
12+
if (allCharsPresentIn(word, allowedCharacters)) {
13+
consistentStrings++;
14+
}
15+
}
16+
return consistentStrings;
17+
}
18+
19+
private Set<Character> getCharacters(String string) {
20+
final Set<Character> set = new HashSet<>();
21+
for (int index = 0 ; index < string.length() ; index++) {
22+
set.add(string.charAt(index));
23+
}
24+
return set;
25+
}
26+
27+
private boolean allCharsPresentIn(String string, Set<Character> allowedChars) {
28+
for (int index = 0 ; index < string.length() ; index++) {
29+
if (!allowedChars.contains(string.charAt(index))) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
}

0 commit comments

Comments
 (0)