通过万岁!!!
- 题目:给你一个字符串allowed,然后再给你一个字符串数组,看一下字符串数组中的字符串的字符是不是都在给定的字符串allowed中,统计都在的个数。
- 思路:首先把allowed的字符放到set集合中。然后遍历字符串数组中字符串的字符即可。
- 思路:哈希表、字符串遍历
java代码
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
Set<Character> set = new HashSet<>();
int ans = 0;
boolean flag;
for (int i = 0; i < allowed.length(); i++) {
set.add(allowed.charAt(i));
}
for (String word : words) {
flag = true;
for (int i = 0; i < word.length(); i++) {
if (!set.contains(word.charAt(i))) {
flag = false;
break;
}
}
if (flag) {
ans++;
}
}
return ans;
}
}
- 总结:题目不是很难,但是时间复杂度不是很理想。