Re: [閒聊] 每日leetcode

作者: enmeitiryous (enmeitiryous)   2024-09-12 08:35:12
今天是easy
題目:1684. count the number of consistent strings
給你一個字串allowed並給你一個由字串組成的vector,求在這個vector中有多少個字串
其中的字母都出現在字串allowed裡面
思路:
用aloowed建unordered-set然後每次確認遇到的字串是否符合條件,如果是的話則ans++
作法基本上大同小異,topic有標bit maniuplation,可以看一下editorial有利用bit去
做到比對字母有沒在allow裡面,蠻酷的
int countConsistentStrings(string allowed, vector<string>& words) {
unordered_set<char> lk;
bool tt=false;
int ans=0;
for(auto k:allowed){
lk.insert(k);
}
for(auto j:words){
tt=true;
for(auto g:j){
if(!lk.count(g)){
tt=false;
}
}
if(tt){
ans++;
}
}
return ans;
}
作者: DJYOMIYAHINA (通通打死)   2023-09-12 08:35:00
早早早
作者: sustainer123 (caster)   2024-09-12 08:43:00
早早早
作者: JerryChungYC (JerryChung)   2024-09-12 08:50:00
早早早
作者: enmeitiryous (enmeitiryous)   2024-09-12 09:02:00

Links booklink

Contact Us: admin [ a t ] ucptt.com