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; } }
标签:char,792,index,int,单词,length,words,序列 From: https://www.cnblogs.com/fulaien/p/17253051.html