首页 > 其他分享 >leetcode-2185. 统计包含给定前缀的字符串

leetcode-2185. 统计包含给定前缀的字符串

时间:2023-01-08 20:35:15浏览次数:79  
标签:string notValid pref 2185 validCnt words leetcode 前缀

简单题,重拳出击

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

相关文章

  • 2185. 统计包含给定前缀的字符串
    2185.统计包含给定前缀的字符串classSolution{publicintprefixCount(String[]words,Stringpref){intres=0;for(Stringword:words......
  • leetcode-409-easy
    LongestPalindromeGivenastringswhichconsistsoflowercaseoruppercaseletters,returnthelengthofthelongestpalindromethatcanbebuiltwiththose......
  • leetcode-345-easy
    ReverseVowelsofaStringGivenastrings,reverseonlyallthevowelsinthestringandreturnit.Thevowelsare'a','e','i','o',and'u',andtheycan......
  • leetcode-643-easy
    MaximumAverageSubarrayIYouaregivenanintegerarraynumsconsistingofnelements,andanintegerk.Findacontiguoussubarraywhoselengthisequalto......
  • leetcode-496-easy
    NextGreaterElementIThenextgreaterelementofsomeelementxinanarrayisthefirstgreaterelementthatistotherightofxinthesamearray.Youar......
  • leetcode-521-easy
    LongestUncommonSubsequenceIGiventwostringsaandb,returnthelengthofthelongestuncommonsubsequencebetweenaandb.Ifthelongestuncommonsubseq......
  • leetcode-551-easy
    StudentAttendanceRecordIYouaregivenastringsrepresentinganattendancerecordforastudentwhereeachcharactersignifieswhetherthestudentwasab......
  • leetcode-485-easy
    MaxConsecutiveOnesGivenabinaryarraynums,returnthemaximumnumberofconsecutive1'sinthearray.Example1:Input:nums=[1,1,0,1,1,1]Output:3E......
  • leetcode-434-easy
    NumberofSegmentsinaStringGivenastrings,returnthenumberofsegmentsinthestring.Asegmentisdefinedtobeacontiguoussequenceofnon-spacech......
  • 【LeetCode数组#4】长度最小的子数组
    长度最小的子数组力扣题目链接(opensnewwindow)给定一个含有n个正整数的数组和一个正整数s,找出该数组中满足其和≥s的长度最小的连续子数组,并返回其长度。如......