Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/09/15 23:29), 編輯推噓3(301)
留言4則, 4人參與, 1年前最新討論串863/1554 (看更多)
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.cc), 來自: 185.213.82.154 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1726414196.A.7DC.html

09/15 23:30, 1年前 , 1F
別卷了
09/15 23:30, 1F

09/15 23:31, 1年前 , 2F
這題我想好久 我好爛
09/15 23:31, 2F

09/15 23:32, 1年前 , 3F
大師
09/15 23:32, 3F

09/15 23:41, 1年前 , 4F
大師
09/15 23:41, 4F
文章代碼(AID): #1cvlrqVS (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cvlrqVS (Marginalman)