792. 匹配子序列的单词数

792. 匹配子序列的单词数

给定字符串 s 和字符串数组 words, 返回  words[i] 中是s的子序列的单词个数 。

字符串的 子序列 是从原始字符串中生成的新字符串,可以从中删去一些字符(可以是none),而不改变其余字符的相对顺序。

  • 例如, “ace”“abcde” 的子序列。

示例 1:

输入: s = "abcde", words = ["a","bb","acd","ace"]
输出: 3
解释: 有三个是 s 的子序列的单词: "a", "acd", "ace"。

Example 2:

输入: s = "dsahjpjauf", words = ["ahjpjau","ja","ahbwzgqnuk","tnmlanowax"]
输出: 2

提示:

  • 1 <= s.length <= 5 * 104
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 50
  • words[i]s 都只由小写字母组成。
class Solution {
    public int numMatchingSubseq(String s, String[] words) {
        char[] checks = s.toCharArray();
        int len = checks.length;
        int count = 0;
        for (int i = 0;i < words.length;i++) {
            char[] chars = words[i].toCharArray();
            count += check(checks,chars);
        }
        return count;
    }

    public int check(char[] big,char[] small) {
        int index = 0;
        for (int i = 0; i < big.length;i++) {
            if (big[i] == small[index]) {
                index++;
                if (index == small.length) return 1;
                continue;
            }
        }
        return 0;
    }
}

 

posted on 2023-03-24 18:48  HHHuskie  阅读(14)  评论(0编辑  收藏  举报

导航