Skip to content

Commit ad81353

Browse files
committed
add sol
1 parent a90f6bc commit ad81353

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

leetcode/daily/2185/sol.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/counting-words-with-a-given-prefix/description/
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func prefixCount(words []string, pref string) int {
8+
count := 0
9+
for i := 0; i < len(words); i++ {
10+
if isPrefix(pref, words[i]) {
11+
count++
12+
}
13+
}
14+
15+
return count
16+
}
17+
18+
func isPrefix(prefix, word string) bool {
19+
n := len(prefix)
20+
m := len(word)
21+
22+
prefixMatch := m >= n && word[:n] == prefix
23+
24+
return prefixMatch
25+
}
26+
27+
func main() {
28+
words := []string{"pay", "attention", "practice", "attend"}
29+
prefix := "at"
30+
fmt.Println(prefixCount(words, prefix))
31+
}

0 commit comments

Comments
 (0)