【每日一题Day81】LC2185统计包含给定前缀的字符串 | 模拟

统计包含给定前缀的字符串【LC2185】

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.

有点玩累啦 开始好好学习吧 尽量白天学完

  • 思路:判断每一个word是否以prefix开头,最后返回满足条件的单词数量。

  • 实现

    • word.startsWith(pref)
    • word.indexOf(pref) == 0
    • word.length() >= m && word.substring(0, m).equals(pref)
    class Solution {
        public int prefixCount(String[] words, String pref) {
            int res = 0, m = pref.length();
            for (String word : words){
                if (word.startsWith(pref)){
                    res++;
                }
                // if (word.indexOf(pref) == 0){
                //     res++;
                // }
                // if (word.length() >= m && word.substring(0, m).equals(pref)){
                //     res++;
                // }
            }
            return res;
        }
    }
    
    • 复杂度
      • 时间复杂度: O ( n m ) O(nm) O(nm), n n nwords数组的长度, m m mprefix前缀的长度
      • 空间复杂度: O ( n ) O(n) O(n)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值