LeetCode 2185. Counting Words With a Given Prefix

59 阅读1分钟

🔗 leetcode.com/problems/co…

题目

  • 给一个字符串数组,返回其中前缀为 pref 的个数

思路

  • 模拟

代码

class Solution {
public:
    int prefixCount(vector<string>& words, string pref) {
        int count = 0;
        for (int i = 0; i < words.size(); i++) {
            bool mark = true;
            for (int j = 0; j < pref.size(); j++) {
                if (pref[j] != words[i][j]) {
                    mark = false;
                    break;
                }
            }
            if (mark) count++;
        }
        return count;
    }
};