Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Jan 9, 2025
1 parent a90f6bc commit ad81353
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions leetcode/daily/2185/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// https://leetcode.com/problems/counting-words-with-a-given-prefix/description/

package main

import "fmt"

func prefixCount(words []string, pref string) int {
count := 0
for i := 0; i < len(words); i++ {
if isPrefix(pref, words[i]) {
count++
}
}

return count
}

func isPrefix(prefix, word string) bool {
n := len(prefix)
m := len(word)

prefixMatch := m >= n && word[:n] == prefix

return prefixMatch
}

func main() {
words := []string{"pay", "attention", "practice", "attend"}
prefix := "at"
fmt.Println(prefixCount(words, prefix))
}

0 comments on commit ad81353

Please sign in to comment.