【题目链接】
题解
- 情感丰富的文字
思路
- 需要灵活使用Python自带的内置函数(zip、all、any等)和工具函数(itertools等)
代码
class Solution:
def expressiveWords(self, S: str, words: List[str]) -> int:
def get_charLs_and_cntLs(s):
if s == '':
return [], []
zipped = [(char, len(list(group))) for char, group in itertools.groupby(s)]
return zip(*zipped)
tar_chars, tar_cnts = get_charLs_and_cntLs(S)
cnt = 0
for word in words:
can_chars, can_cnts = get_charLs_and_cntLs(word)
if tar_chars != can_chars: continue
cnt += all([tar_cnt >= max(can_cnt, 3) or tar_cnt == can_cnt for
tar_cnt, can_cnt in zip(tar_cnts, can_cnts)])
return cnt