Java C++題解leetcode 1684統(tǒng)計一致字符串的數(shù)目示例
更新時間:2023年01月16日 11:47:21 作者:AnjaVon
這篇文章主要為大家介紹了Java C++題解leetcode 1684統(tǒng)計一致字符串的數(shù)目示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
題目
思路:模擬
- 用一個哈希表記錄可出現(xiàn)的字母,然后逐一遍歷每個單詞每個字母,符合條件則結(jié)果加一。
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++; } return res; } }
C++
class Solution { public: int countConsistentStrings(string allowed, vector<string>& words) { int hash[26] = {0}; 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 res; } };
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_bytes().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 = false; continue; } } if ok { res += 1; } } res } }
以上就是Java C++題解leetcode 1684統(tǒng)計一致字符串的數(shù)目示例的詳細(xì)內(nèi)容,更多關(guān)于Java C++統(tǒng)計一致字符串?dāng)?shù)目的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring源碼學(xué)習(xí)之動態(tài)代理實現(xiàn)流程
這篇文章主要給大家介紹了關(guān)于Spring源碼學(xué)習(xí)之動態(tài)代理實現(xiàn)流程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03集成apollo動態(tài)日志取締logback-spring.xml配置
這篇文章主要為大家介紹了集成apollo動態(tài)日志取締logback-spring.xml配置的過程內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02用SpringBoot Admin監(jiān)控SpringBoot程序
這篇文章主要介紹了用SpringBoot Admin監(jiān)控SpringBoot程序,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-10-10