(This question is a variation of the LeetCode question 3926. Count Valid Word Occurrences. If you haven't completed that question yet, it is recommended to solve it first.)
You are given a string text and an array of source phrases sources. A source occurs when its complete sequence of words appears consecutively in the text with the same letter case. A source cannot match part of a word.
Count every valid starting position, including overlapping occurrences. Equal source strings at different indices are counted separately.
Return an array containing the occurrence count for each source, in the order of sources. A source that does not occur has count 0.
Constraints:
- 1 ≤
text.length≤ 1,000. - 1 ≤
sources.length≤ 50. - 1 ≤
sources[i].length≤ 100. textand each source contain only English letters, digits, and single spaces between words, with no leading or trailing spaces.
Example 1:
Input: text = "a a a networking", sources = ["a a", "a", "network"] Output: [2, 3, 0] Explanation: The phrase "a a" occurs twice, with overlapping occurrences. "a" occurs three times, while "network" is only part of "networking" and does not match a whole word.
Example 2:
Input: text = "red red red blue red red", sources = ["red red", "red", "red red", "blue red"] Output: [3, 5, 3, 1]
Example 3:
Input: text = "cat cats Cat cat2 2cat cat", sources = ["cat", "Cat", "cats", "2cat", "cat2"] Output: [2, 1, 1, 1, 1]
