Skip to content

Latest commit

 

History

History
61 lines (49 loc) · 1.78 KB

File metadata and controls

61 lines (49 loc) · 1.78 KB

792. 匹配子序列的单词数

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

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

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

示例 1:

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

示例 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 都只由小写字母组成。
  • s and words[i] consist of only lowercase English letters.

题解 (Rust)

1. 题解

impl Solution {
    pub fn num_matching_subseq(s: String, words: Vec<String>) -> i32 {
        let mut indices = vec![vec![]; 26];
        let mut ret = 0;

        for (i, c) in s.bytes().enumerate() {
            indices[(c - b'a') as usize].push(i);
        }

        for word in &words {
            let mut i = 0;
            let mut flag = true;

            for c in word.bytes() {
                match indices[(c - b'a') as usize].binary_search(&i) {
                    Err(j) if j == indices[(c - b'a') as usize].len() => {
                        flag = false;
                        break;
                    }
                    Ok(j) | Err(j) => i = indices[(c - b'a') as usize][j] + 1,
                }
            }

            ret += flag as i32;
        }

        ret
    }
}