-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2185-CountingWordsWithAGivenPrefix.go
More file actions
49 lines (40 loc) · 1.58 KB
/
2185-CountingWordsWithAGivenPrefix.go
File metadata and controls
49 lines (40 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
// 2185. Counting Words With a Given Prefix
// You are given an array of strings words and a string pref.
// Return the number of strings in words that contain pref as a prefix.
// A prefix of a string s is any leading contiguous substring of s.
// Example 1:
// Input: words = ["pay","attention","practice","attend"], pref = "at"
// Output: 2
// Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
// Example 2:
// Input: words = ["leetcode","win","loops","success"], pref = "code"
// Output: 0
// Explanation: There are no strings that contain "code" as a prefix.
// Constraints:
// 1 <= words.length <= 100
// 1 <= words[i].length, pref.length <= 100
// words[i] and pref consist of lowercase English letters.
import "fmt"
import "strings"
func prefixCount(words []string, pref string) int {
res := 0
for _, word := range words {
if strings.Contains(word, pref) && strings.HasPrefix(word, pref) {
res++
}
}
return res
}
func main() {
// Example 1:
// Input: words = ["pay","attention","practice","attend"], pref = "at"
// Output: 2
// Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
fmt.Println(prefixCount([]string{"pay","attention","practice","attend"}, "at")) // 2
// Example 2:
// Input: words = ["leetcode","win","loops","success"], pref = "code"
// Output: 0
// Explanation: There are no strings that contain "code" as a prefix.
fmt.Println(prefixCount([]string{"leetcode","win","loops","success"}, "code")) // 0
}