LeetCode(Map)1684. Count the Number of Consistent Strings

墨蓝 2023-09-24 16:39 240阅读 0赞

1.问题

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.

Example 1:

Input: allowed = “ab”, words = [“ad”,“bd”,“aaab”,“baa”,“badab”]
Output: 2
Explanation: Strings “aaab” and “baa” are consistent since they only contain characters ‘a’ and ‘b’.

Example 2:

Input: allowed = “abc”, words = [“a”,“b”,“c”,“ab”,“ac”,“bc”,“abc”]
Output: 7
Explanation: All strings are consistent.

Example 3:

Input: allowed = “cad”, words = [“cc”,“acd”,“b”,“ba”,“bac”,“bad”,“ac”,“d”]
Output: 4
Explanation: Strings “cc”, “acd”, “ac”, and “d” are consistent.

Constraints:

  • 1 <= words.length <= 104
  • 1 <= allowed.length <= 26
  • 1 <= words[i].length <= 10
    The characters in allowed are distinct.
    words[i] and allowed contain only lowercase English letters.

2. 解题思路

方法1:

1.定义result初始为0
2.for循环遍历words,如果allowed包含words中char数组元素为ture,result+1,否者为false,result不变
3.返回result

方法2:

1.定义result初始为0
2.新建一个hashset,将allowed.toCharArray()存入set中
3.for循环遍历words,如果set包含words中char数组元素为ture,result+1,否者为false,result不变
4.返回result

方法3:

1.定义长度为26的数组
2.for循环遍历,ch-‘a’是元素在alpha数组的位置
3.遍历words的char数组元素,alpha[ch-‘a’]是元素在alpha中的位置,<=0是没有出现在数组中,T:继续循环,直到出现+1
4.返回count

3. 代码

代码1:

  1. class Solution {
  2. public int countConsistentStrings(String allowed, String[] words) {
  3. int result =0;//1.定义result初始为0
  4. for(String w:words){
  5. //2.for循环遍历words,如果allowed包含words中char数组元素为ture,result+1,否者为false,result不变
  6. boolean flag = true;
  7. for(char c: w.toCharArray()){
  8. if(!allowed.contains(String.valueOf(c))){
  9. flag=false;
  10. }
  11. }
  12. if(flag) result+=1;
  13. }
  14. return result;//3.返回result
  15. }
  16. }

代码2:

  1. class Solution {
  2. public int countConsistentStrings(String allowed, String[] words) {
  3. int result =0;//1.定义result初始为0
  4. Set<Character> set = new HashSet<>();//2.新建一个hashset,将allowed.toCharArray()存入set中
  5. for(char a: allowed.toCharArray()){
  6. set.add(a);
  7. }
  8. for(String w:words){
  9. //3.for循环遍历words,如果set包含words中char数组元素为ture,result+1,否者为false,result不变
  10. boolean flag = true;
  11. for(char c: w.toCharArray()){
  12. if(!set.contains(c)){
  13. //注意:这是set.contains(c)是charter.contains(charter)
  14. flag=false;
  15. }
  16. }
  17. if(flag) result+=1;
  18. }
  19. return result;//4.返回result
  20. }
  21. }

方法3:

  1. class Solution {
  2. public int countConsistentStrings(String allowed, String[] words) {
  3. int [] alpha = new int[26];//1.定义长度为26的数组
  4. for (int i=0; i < allowed.length(); i++)//2.for循环遍历,ch-'a'是元素在alpha数组的位置
  5. {
  6. char ch = allowed.charAt(i);
  7. alpha[ch-'a']++;
  8. }
  9. int count = 0;
  10. T: for (String word : words){
  11. //3.遍历words的char数组元素,alpha[ch-'a']是元素在alpha中的位置,<=0是没有出现在数组中,T:继续循环,直到出现+1
  12. for ( char ch:word.toCharArray()) {
  13. if (alpha[ch-'a'] <= 0) continue T;
  14. }
  15. count++;
  16. }
  17. //
  18. return count;//4.返回count
  19. }
  20. }

解题思路基本相同,不同的地方在于查询方式后结果+1;相比上面代码下面的代码比较容易理解

  1. class Solution {
  2. public int countConsistentStrings(String allowed, String[] words) {
  3. boolean alph[]=new boolean[26];//1.新建boolenan的数组
  4. for(char c:allowed.toCharArray()){
  5. //2.如果allow的char元素在a数组元素中,为true
  6. alph[c-'a']=true;
  7. }
  8. int count=0;
  9. for(String i:words){
  10. //3.for循环遍历words,a[c-'a']是元素是否在alph中,如果alph[c-'a']为ture,result+1,否者为false,result不变
  11. boolean flag=true;
  12. for(char c:i.toCharArray()){
  13. if(!alph[c-'a']){
  14. flag=false;
  15. }
  16. }
  17. if(flag){
  18. count++;
  19. }
  20. }
  21. return count;//4.返回count
  22. }
  23. }

发表评论

表情:
评论列表 (有 0 条评论,240人围观)

还没有评论,来说两句吧...

相关阅读