Re: [閒聊] 每日leetcode

作者: dont   2024-09-15 23:29:52
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
```
作者: ILoveErr (英梨梨我老婆)   2024-09-15 23:30:00
別卷了
作者: JenniferLope (ㄚ)   2024-09-15 23:31:00
這題我想好久 我好爛
作者: sustainer123 (caster)   2024-09-15 23:32:00
大師
作者: Che31128 (justjoke)   2024-09-15 23:41:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com