Carregando

Problema do LeetCode#1684

Count the Number of Consistent Strings

Resolvido em 1 linguagemJava

Resolver no LeetCode(abre em uma nova aba)

Solução

27 linhas
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}

Quer uma explicação desta solução em Java?

Receba uma explicação passo a passo da ideia por trás do código.

Todos os problemas

Os enunciados dos problemas pertencem ao LeetCode e estão em leetcode.com. Não temos vínculo com o LeetCode.