Re: [閒聊] 每日leetcode
看板Marginalman作者JerryChungYC (JerryChung)時間1年前 (2024/09/12 08:48)推噓0(0推 0噓 6→)留言6則, 2人參與討論串848/1548 (看更多)
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 就好 所以多一行
只是這成績真的好爛 :(
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.251.52.67 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1726102106.A.887.html
→
09/12 08:49,
1年前
, 1F
09/12 08:49, 1F
→
09/12 08:49,
1年前
, 2F
09/12 08:49, 2F
※ 編輯: JerryChungYC (60.251.52.67 臺灣), 09/12/2024 08:50:53
→
09/12 08:55,
1年前
, 3F
09/12 08:55, 3F
→
09/12 08:58,
1年前
, 4F
09/12 08:58, 4F
→
09/12 08:59,
1年前
, 5F
09/12 08:59, 5F
→
09/12 09:03,
1年前
, 6F
09/12 09:03, 6F
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
allowed_set = set(allowed)
count = 0
for word in words:
c = False
for w in word:
if w not in allowed:
c = True
break
if c:
continue
count += 1
return count
※ 編輯: JerryChungYC (60.251.52.67 臺灣), 09/12/2024 09:09:16
討論串 (同標題文章)
完整討論串 (本文為第 848 之 1548 篇):