給你一個由不同字符組成的字符串 allowed 和一個字符串數組 words 。如果一個字符串的每一個字符都在 allowed 中,就稱這個字符串是 一致字符串 。
請你返回 words 數組中 一致字符串 的數目。
示例 1:
輸入:allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
輸出:2
解釋:字符串 "aaab" 和 "baa" 都是一致字符串,因為它們只包含字符 'a' 和 'b' 。
示例 2:
輸入:allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
輸出:7
解釋:所有字符串都是一致的。
示例 3:
輸入:allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
輸出:4
解釋:字符串 "cc","acd","ac" 和 "d" 是一致字符串。
提示:
1 <= words.length <= 104
1 <= allowed.length <= 26
1 <= words[i].length <= 10
allowed 中的字符 互不相同 。
words[i] 和 allowed 只包含小寫英文字母。
java代碼:
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
int count=0;
for(int i=0;i<words.length;i++){
boolean flag = true;
for(int j=0;j<words[i].length();j++){
String s = words[i].substring(j,j+1);
if(!(allowed.contains(s))){
flag = false;
break;
}
}
if(flag){
count++;
}
}
return count;
}
}