Re: [闲聊] 每日leetcode

楼主: sustainer123 (caster)   2024-09-12 08:44:31
※ 引述《enmeitiryous (enmeitiryous)》之铭言:
: 今天是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;
: }
思路:
就一个字一个字确认 好像也没更快的方法
Python Code:
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
result = 0
for word in words:
result += 1
for c in word:
if c not in allowed:
result -= 1
break
return result
作者: JerryChungYC (JerryChung)   2024-09-12 08:50:00
早早

Links booklink

Contact Us: admin [ a t ] ucptt.com