简单题,重拳出击
func prefixCount(words []string, pref string) int {
validCnt := 0
for _, w := range words {
notValid := false
if len(w) < len(pref) {
continue
}
for k, c := range pref {
if c != rune(w[k]) {
notValid = true
}
}
if notValid {
continue
}
validCnt++
}
return validCnt
}
使用strings
库中的HasPrefix
函数直接判断一下:
func prefixCount(words []string, pref string) int {
validCnt := 0
for _, w := range words {
if strings.HasPrefix(w, pref) {
validCnt++
}
}
return validCnt
}
参考
2185. 统计包含给定前缀的字符串 - 力扣(Leetcode)
标签:string,notValid,pref,2185,validCnt,words,leetcode,前缀 From: https://www.cnblogs.com/wudanyang/p/17035273.html