Count the Number of Consistent Strings
给一个字符串allowed和一个字符串组words, 问words里面是不是由allowed里的字符组成.
查存在用set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Solution { public int countConsistentStrings(String allowed, String[] words) { Set<Character> set = new HashSet<>(); for(char c :allowed.toCharArray()) set.add(c); int count = 0; for(String w : words) { boolean all = true; for(char c : w.toCharArray()) { if(!set.contains(c)){ all = false; break; } } if(all) count++; } return count; } } |