0
点赞
收藏
分享

微信扫一扫

力扣 6008. 统计包含给定前缀的字符串

大南瓜鸭 2022-02-27 阅读 67

题目

给你一个字符串数组 words 和一个字符串 pref 。

返回 words 中以 pref 作为 前缀 的字符串的数目。

字符串 s 的 前缀 就是 s 的任一前导连续字符串。

示例

输入:words = [“pay”,“attention”,“practice”,“attend”], pref = “at”
输出:2
解释:以 “at” 作为前缀的字符串有两个,分别是:“attention” 和 “attend” 。

输入:words = [“leetcode”,“win”,“loops”,“success”], pref = “code”
输出:0
解释:不存在以 “code” 作为前缀的字符串。

方法1

Java实现
class Solution {
    public int prefixCount(String[] words, String pref) {
        int ans = 0;
        for (String str : words) {
            if (str.startsWith(pref)) ans++;
        }
        
        return ans;
    }
}

在这里插入图片描述

举报

相关推荐

0 条评论