From 967ca0f1a233ed2c858a994fcb712618362144d2 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 15 Sep 2024 17:55:26 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1371.=20Find=20the=20Longest?= =?UTF-8?q?=20Substring=20Containing=20Vowels=20in=20Even=20Counts=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...string-containing-vowels-in-even-counts.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 go/find-the-longest-substring-containing-vowels-in-even-counts.go diff --git a/go/find-the-longest-substring-containing-vowels-in-even-counts.go b/go/find-the-longest-substring-containing-vowels-in-even-counts.go new file mode 100644 index 0000000..d2dda97 --- /dev/null +++ b/go/find-the-longest-substring-containing-vowels-in-even-counts.go @@ -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 +}