classSolution: deffindTheLongestSubstring(self, s: str) -> int: for i in range(len(s), 0, -1): for j in range(len(s) - i + 1): sub = s[j:j + i] has_odd_vowel = False for vowel in ['a', 'e', 'i', 'o', 'u']: if sub.count(vowel) % 2 != 0: has_odd_vowel = True break ifnot has_odd_vowel: return i return0
classSolution: i_mapper = { "a": 0, "e": 1, "i": 2, "o": 3, "u": 4 } defcheck(self, s, pre, l, r): for i in range(5): if s[l] in self.i_mapper and i == self.i_mapper[s[l]]: cnt = 1 else: cnt = 0 if (pre[r][i] - pre[l][i] + cnt) % 2 != 0: returnFalse returnTrue deffindTheLongestSubstring(self, s: str) -> int: n = len(s)
pre = [[0] * 5for _ in range(n)]
# pre for i in range(n): for j in range(5): if s[i] in self.i_mapper and self.i_mapper[s[i]] == j: pre[i][j] = pre[i - 1][j] + 1 else: pre[i][j] = pre[i - 1][j] for i in range(n - 1, -1, -1): for j in range(n - i): if self.check(s, pre, j, i + j): return i + 1 return0
classSolution: deffindTheLongestSubstring(self, s: str) -> int: mapper = { "a": 1, "e": 2, "i": 4, "o": 8, "u": 16 } seen = {0: -1} res = cur = 0
for i in range(len(s)): if s[i] in mapper: cur ^= mapper.get(s[i]) # 全部奇偶性都相同,相减一定都是偶数 if cur in seen: res = max(res, i - seen.get(cur)) else: seen[cur] = i return res