leetcode-2185. 统计包含给定前缀的字符串
简单题,重拳出击
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
}
参考
本文来自博客园,作者:吴丹阳-V,转载请注明原文链接:https://www.cnblogs.com/wudanyang/p/17035273.html