开发者

Java C++题解leetcode 1684统计一致字符串的数目示例

目录
  • 题目
    • 思路:模拟
  • Java
    • C++
      • Rust

        题目

        题目要求

        Java C++题解leetcode 1684统计一致字符串的数目示例

        Java C++题解leetcode 1684统计一致字符串的数目示例

        思路:模拟

        • 用一个哈希表记录可出现的字母,然后逐一遍历每个单词每个字母,符合条件则结果加一。

        Java

        class Solution {
            public int countConsistentStrings(String allowed, String[] words) {
                boolean[] hash = new boolean[26];
                for (var a : allowed.toCharArray())
                    hash[a - 'a'] = true;
                int res = 0;
                stop : for (var word : words) {
                    for (var w : word.toCharArray()) {
                        if (!hash[w - 'a'])
                            continue stop;
                    }
                    res++;
            开发者_Python入门    }
                return res;
            }
        }
        

        Java C++题解leetcode 1684统计一致字符串的数目示例

        C++

        class Solution {
        public:
            int countConsistentStrings(string allowed, vector<string>& words) {
                int hash[26] = {0};
              android  for (auto a : allowed)
                    hash[a - 'a'] = true;
                int res = 0;
                for (auto& word : words) {
                    bool ok = true;
                    for (auto w : word) {
                        if (!hash[w - 'a']) {
                            ok = false;
                            continue;
                        }
                    }
                    if (ok)
                        res++;
                }
                return rehttp://www.devze.coms;
            }
        };
        
        python

        Java C++题解leetcode 1684统计一致字符串的数目示例

        Rust

        impl Solution {
            pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
                let mut hash = vec![false; 26];
                for a in allowed.as_bytesjs().iter() {
                    hash[(a - b'a') as usize] = true;
                }
                let mut res = 0;
                for word in words {
                    let mut ok = true;
                    for w in word.as_bytes().iter() {
                        if !hash[(w - b'a') as usize] {
                            ok = falseandroid;
                            continue;
                        }
                    }
                    if ok {
                        res += 1;
                    }
                }
                res
            }
        }
        

        Java C++题解leetcode 1684统计一致字符串的数目示例

        以上就是Java C++题解leetcode 1684统计一致字符串的数目示例的详细内容,更多关于Java C++统计一致字符串数目的资料请关注我们其它相关文章!

        0

        上一篇:

        下一篇:

        精彩评论

        暂无评论...
        验证码 换一张
        取 消

        最新开发

        开发排行榜