1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00

go: add «1371. Find the Longest Substring Containing Vowels in Even Counts»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-15 17:55:26 +02:00
parent 5449ec348a
commit 967ca0f1a2
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,40 @@
package main
func findTheLongestSubstring(s string) int {
getMask := func(c rune) int {
switch c {
case 'a':
return 1
case 'e':
return 2
case 'i':
return 4
case 'o':
return 8
case 'u':
return 16
default:
return 0
}
}
longest := 0
indices := make([]int, 32)
for i, _ := range indices {
indices[i] = -1
}
xor := 0
for i, c := range s {
xor ^= getMask(c)
if indices[xor] == -1 && xor != 0 {
indices[xor] = i
}
longest = max(longest, i-indices[xor])
}
return longest
}