1967. Number of Strings That Appear as Substrings in Word

Given an array of strings patterns and a string word, return the number of strings in patterns that exist as a substring in word.

A substring is a contiguous sequence of characters within a string.

 

Example 1:

Input: patterns = ["a","abc","bc","d"], word = "abc"
Output: 3
Explanation:
- "a" appears as a substring in "abc".
- "abc" appears as a substring in "abc".
- "bc" appears as a substring in "abc".
- "d" does not appear as a substring in "abc".
3 of the strings in patterns appear as a substring in word.

Example 2:

Input: patterns = ["a","b","c"], word = "aaaaabbbbb"
Output: 2
Explanation:
- "a" appears as a substring in "aaaaabbbbb".
- "b" appears as a substring in "aaaaabbbbb".
- "c" does not appear as a substring in "aaaaabbbbb".
2 of the strings in patterns appear as a substring in word.

Example 3:

Input: patterns = ["a","a","a"], word = "ab"
Output: 3
Explanation: Each of the patterns appears as a substring in word "ab".

 

Constraints:

  • 1 <= patterns.length <= 100
  • 1 <= patterns[i].length <= 100
  • 1 <= word.length <= 100
  • patterns[i] and word consist of lowercase English letters.

1967. Number of Strings That Appear as Substrings in Word
struct Solution;

impl Solution {
    fn num_of_strings(patterns: Vec<String>, word: String) -> i32 {
        let mut res = 0;
        for p in patterns {
            if word.contains(&p) {
                res += 1;
            }
        }
        res
    }
}

#[test]
fn test() {
    let patterns = vec_string!["a", "abc", "bc", "d"];
    let word = "abc".to_string();
    let res = 3;
    assert_eq!(Solution::num_of_strings(patterns, word), res);
    let patterns = vec_string!["a", "b", "c"];
    let word = "aaaaabbbbb".to_string();
    let res = 2;
    assert_eq!(Solution::num_of_strings(patterns, word), res);
    let patterns = vec_string!["a", "a", "a"];
    let word = "ab".to_string();
    let res = 3;
    assert_eq!(Solution::num_of_strings(patterns, word), res);
}