Skip to content

Commit 63ba26f

Browse files
author
openset
committed
Add: Compare Strings by Frequency of the Smallest Character
1 parent e56da2a commit 63ba26f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package problem1170
2+
3+
func numSmallerByFrequency(queries []string, words []string) []int {
4+
ans, m := make([]int, len(queries)), make([]int, len(words))
5+
for i, w := range words {
6+
m[i] = f(w)
7+
}
8+
for i, q := range queries {
9+
t := f(q)
10+
for _, n := range m {
11+
if t < n {
12+
ans[i]++
13+
}
14+
}
15+
}
16+
return ans
17+
}
18+
19+
func f(s string) int {
20+
m, p := [26]int{}, byte(25)
21+
for i := 0; i < len(s); i++ {
22+
k := s[i] - 'a'
23+
m[k]++
24+
if p > k {
25+
p = k
26+
}
27+
}
28+
return m[p]
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package problem1170
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type testType struct {
9+
in []string
10+
w []string
11+
want []int
12+
}
13+
14+
func TestNumSmallerByFrequency(t *testing.T) {
15+
tests := [...]testType{
16+
{
17+
in: []string{"cbd"},
18+
w: []string{"zaaaz"},
19+
want: []int{1},
20+
},
21+
{
22+
in: []string{"bbb", "cc"},
23+
w: []string{"a", "aa", "aaa", "aaaa"},
24+
want: []int{1, 2},
25+
},
26+
}
27+
for _, tt := range tests {
28+
got := numSmallerByFrequency(tt.in, tt.w)
29+
if !reflect.DeepEqual(got, tt.want) {
30+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)