[코테 풀이] Count the Number of Consistent Strings

시내·2024년 6월 14일
0

Q_1684) Count the Number of Consistent Strings

출처 : https://leetcode.com/problems/count-the-number-of-consistent-strings/

You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed.

Return the number of consistent strings in the array words.

class Solution {
    public int countConsistentStrings(String allowed, String[] words) {
        int count = 0;
        for (int i = 0; i < words.length; i++) {
            boolean flag = true;
            Set<Character> set = new HashSet<>();
            for (int a = 0; a < allowed.length(); a++) {
                set.add(allowed.charAt(a));
            }
            for (int j = 0; j < words[i].length(); j++) {
                if (!set.contains(words[i].charAt(j))) {
                    flag = false;
                    break;
                }
            }
            if (flag) count++;
        }
        return count;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글