Loading

LeetCode problem#1684

Count the Number of Consistent Strings

Solved in 1 languageJava

Solve it on LeetCode(opens in a new tab)

Solution

27 lines
1class Solution {
2    // time O(n^2)
3    // space O(n)
4    public int countConsistentStrings(String allowed, String[] words) {
5        HashSet<Character> hashSet = new HashSet<Character>();
6        int count = 0;
7
8        for (int i = 0; i < allowed.length(); i++) {
9            hashSet.add(allowed.charAt(i));
10        }
11
12        outerloop:
13        for (int i = 0; i < words.length; i++) {
14            String word = words[i];
15            
16            for (int j = 0; j < word.length(); j++) {
17                if (!hashSet.contains(word.charAt(j))) {
18                    continue outerloop;
19                }
20            }
21
22            count++;
23        }
24
25        return count;
26    }
27}

Want a walkthrough of this Java solution?

Get a step-by-step explanation of the idea behind the code.

All problems

Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.