Skip to content

Commit 788bf88

Browse files
author
Shuo
authored
Merge pull request #729 from openset/develop
Add: Compare Strings by Frequency of the Smallest Character
2 parents b9621a2 + 51ee66b commit 788bf88

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package problem1170
2+
3+
import "sort"
4+
5+
func numSmallerByFrequency(queries []string, words []string) []int {
6+
ans, m := make([]int, len(queries)), make([]int, len(words))
7+
for i, w := range words {
8+
m[i] = f(w)
9+
}
10+
sort.Ints(m)
11+
for i, q := range queries {
12+
t := f(q)
13+
for j := len(m) - 1; j >= 0; j-- {
14+
if t >= m[j] {
15+
break
16+
}
17+
ans[i]++
18+
}
19+
}
20+
return ans
21+
}
22+
23+
func f(s string) int {
24+
m, p := [26]int{}, byte(25)
25+
for i := 0; i < len(s); i++ {
26+
k := s[i] - 'a'
27+
m[k]++
28+
if p > k {
29+
p = k
30+
}
31+
}
32+
return m[p]
33+
}
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)