Re: [閒聊] 每日leetcode
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
討論串 (同標題文章)
完整討論串 (本文為第 849 之 1548 篇):