Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/09/12 12:21), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串849/1548 (看更多)
1684. Count the Number of Consistent Strings ## 思路 先把allowed轉成set 再檢查每個word ## Code ```python class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed = set(allowed) res = len(words) for word in words: for ch in word: if ch not in allowed: res -= 1 break return res ``` 2506. Count Pairs Of Similar Strings ## 思路 hash table存相似字串的次數 key: 用bitmask 記錄字串包含的字元 ## Code ```python class Solution: def similarPairs(self, words: List[str]) -> int: count = defaultdict(int) res = 0 for word in words: mask = 0 for ch in word: mask |= 1 << (ord(ch) - ord('a')) res += count[mask] count[mask] += 1 return res ``` -- https://i.imgur.com/kyBhy6o.jpeg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.253 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1726114907.A.BBD.html
文章代碼(AID): #1cucnRkz (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cucnRkz (Marginalman)