Re: [闲聊] 每日leetcode

楼主: JerryChungYC (JerryChung)   2024-09-12 08:48:24
https://leetcode.com/problems/count-the-number-of-consistent-strings
1684. Count the Number of Consistent Strings
给一个 由不同字符组成的 allowed 字串 跟一组字串数组 words
数组中的字串 如果所有字符都在 allowed 里面的话 代表是 consistent
求数组中 consistent 的数量
Example 1:
Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
Output: 2
Explanation: 只有 "aaab" 跟 "baa" 是由 'a' 和 'b' 组成的
Example 2:
Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
Output: 7
Explanation: 全部字串都是 consistent
Example 3:
Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
Output: 4
Explanation: "cc", "acd", "ac" 和 "d" 是 conststent
Python Code:
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
a_set = set(allowed)
return sum(1 for word in words if all(w in a_set for w in set(word)))
allowed 做一次 set 就好 所以多一行
只是这成绩真的好烂 :(
楼主: JerryChungYC (JerryChung)   2024-09-12 08:49:00
看了一下all应该也是遇到False就直接跳出 所以就不自己写for了word不转set好像更好 哀 算了count好像比sum好 哀 算了
作者: sustainer123 (caster)   2024-09-12 08:59:00
早早早
楼主: JerryChungYC (JerryChung)   2024-09-12 09:03:00
用-=比用all生成器好 哀 算了

Links booklink

Contact Us: admin [ a t ] ucptt.com