Skip to content

Commit 6928fca

Browse files
committed
feat(solutions): easy: find-most-frequent-vowel-and-consonant.go
1 parent 69e3631 commit 6928fca

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package easy
2+
3+
// https://leetcode.com/problems/find-most-frequent-vowel-and-consonant
4+
func maxFreqSum(s string) int {
5+
// 1 <= s.length <= 100
6+
// s consists of lowercase English letters only.
7+
var letters [26]byte
8+
9+
for i := 0; i < len(s); i++ {
10+
letters[s[i]-'a']++
11+
}
12+
13+
var maxVowel byte
14+
var maxLetter byte
15+
16+
// a=0, e=4, i=8, o=14, u=20
17+
const vowelMask = (1 << 0) | (1 << 4) | (1 << 8) | (1 << 14) | (1 << 20)
18+
19+
for i := 0; i < len(letters); i++ {
20+
if (vowelMask & (1 << i)) != 0 { // mask with only the i-th bit set
21+
maxVowel = max(maxVowel, letters[i])
22+
} else {
23+
maxLetter = max(maxLetter, letters[i])
24+
}
25+
}
26+
27+
return int(maxVowel + maxLetter)
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package easy
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestMaxFreqSum(t *testing.T) {
10+
cases := []struct {
11+
s string
12+
expected int
13+
}{
14+
{"successes", 6},
15+
{"aeiaeia", 3},
16+
}
17+
18+
for _, tc := range cases {
19+
assert.Equal(t, tc.expected, maxFreqSum(tc.s),
20+
"input = %v", tc.s,
21+
)
22+
}
23+
}

0 commit comments

Comments
 (0)