Count Valid Word Occurrences Problem
Description
LeetCode Problem 3926.
You are given an array of strings chunks. Concatenate all strings in chunks in order to form a string s.
You are also given an array of strings queries.
A joiner hyphen is a hyphen character '-' in s whose previous and next characters both exist and are lowercase English letters.
A word is a maximal substring of s consisting only of lowercase English letters and joiner hyphens.
All other characters, including spaces and hyphens that are not joiner hyphens, are treated as separators.
Return an integer array ans, where ans[i] is the number of times queries[i] appears as a word in s.
Example 1:
Input: chunks = ["hello wor","ld hello"], queries = ["hello","world","wor"]
Output: [2,1,0]
Explanation:
- After concatenating all strings in chunks, s = "hello world hello".
- The words are "hello", "world", and "hello".
- The substring "wor" appears inside "world", but it is not a full word.
Example 2:
Input: chunks = ["a-b a--b ","a-","b"], queries = ["a-b","a","b"]
Output: [2,1,1]
Explanation:
- After concatenating all strings in chunks, s = "a-b a--b a-b".
- In "a-b", the hyphen is a joiner hyphen because it is between two lowercase English letters, so "a-b" is one word.
- In "a--b", neither hyphen is a joiner hyphen, so it is split into the words "a" and "b".
- Therefore, the words are "a-b", "a", "b", and "a-b".
Example 3:
Input: chunks = ["-cat dog- mouse"], queries = ["cat","dog","mouse","cat-dog"]
Output: [1,1,1,0]
Explanation:
- After concatenating all strings in chunks, s = "-cat dog- mouse".
- The leading hyphen before "cat" and the trailing hyphen after "dog" are not joiner hyphens, so they are separators.
- The words are "cat", "dog", and "mouse".
Constraints:
1 <= chunks.length <= 10^51 <= chunks[i].length <= 10^5- The total length of all strings in
chunksdoes not exceed10^5. chunks[i]consists only of lowercase English letters, spaces, and'-'.1 <= queries.length <= 10^51 <= queries[i].length <= 10^5- The total length of all strings in
queriesdoes not exceed10^5. queries[i]consists only of lowercase English letters and'-'.queries[i]is a valid word: it does not start or end with'-', and it does not contain two consecutive hyphens.
Solution
Approach: Scan and Count Words
Join all chunks into one string s. Then scan it from left to right:
- Skip spaces and hyphens: a word must start with a letter.
- From a letter, extend the word until the next space, or until a hyphen that is not a joiner. A hyphen is a joiner only when the next character exists and is a letter, and the previous character is a letter because we are inside the word.
Count every word found in a hash map. Each query is then answered by a lookup (0 if the word never appeared). Queries never start or end with a hyphen and have no double hyphen, so they match words exactly.
Complexity: O(N + M) time and O(N) extra space, where N is the total length of chunks and M the total length of queries.
from collections import defaultdict
class Solution:
def countWordOccurrences(self, chunks: List[str], queries: List[str]) -> List[int]:
s = "".join(chunks)
n = len(s)
cnt = defaultdict(int)
i = 0
while i < n:
if s[i] in " -":
i += 1
continue
j = i
while (
j < n
and s[j] != " "
and (s[j] != "-" or (j + 1 < n and s[j + 1] not in " -"))
):
j += 1
cnt[s[i:j]] += 1
i = j
return [cnt[q] for q in queries]