Skip to content

Commit 138d828

Browse files
committed
prefixCount
1 parent a75cd1e commit 138d828

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package index
2+
3+
func PrefixCount(words []string, pref string) int {
4+
return prefixCount(words, pref)
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/masx200/leetcode-test/counting-words-with-a-given-prefix
2+
3+
go 1.19
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package index
2+
3+
import "strings"
4+
5+
func prefixCount(words []string, pref string) int {
6+
return ReduceSlice(words, func(a int, v string) int {
7+
if strings.HasPrefix(v, pref) {
8+
return a + 1
9+
}
10+
return a
11+
}, 0)
12+
}
13+
14+
func ReduceSlice(s []string, c func(a int, v string) int, i int) int {
15+
16+
for _, v := range s {
17+
i = c(i, v)
18+
}
19+
return i
20+
}

0 commit comments

Comments
 (0)