1371. Find the Longest Substring Containing Vowels in Even Counts ## 思路 用bitmask紀錄當前aeiou的奇偶個數 hash table存mask第一次遇到的index 如果s[:j]的mask最早出現在index i 表示s[i+1:j] 之間的母音都是偶數個 ## Code ```python class Solution: def findTheLongestSubstring(self, s: str) -> int: first_seen = defaultdict(int) first_seen[0] = -1 vowels = {} for i, vowel in enumerate('aeiou'): vowels[vowel] = 1 << i res = mask = 0 for idx, ch in enumerate(s): if ch in vowels: mask ^= vowels[ch] if mask not in first_seen: first_seen[mask] = idx else: res = max(res, idx - first_seen[mask]) return res ``` -- https://i.imgur.com/kyBhy6o.jpeg
-- ※ 發信站: 批踢踢實業坊(ptt.org.tw), 來自: 185.213.82.154 (臺灣) ※ 文章網址: https://ptt.org.tw/Marginalman/M.1726414196.A.7DC
ILoveErr: 別卷了 09/15 23:30
JenniferLope: 這題我想好久 我好爛 09/15 23:31
sustainer123: 大師 09/15 23:32
Che31128: 大師 09/15 23:41