Hackerrank - Regex Find a Word Solution
Given sentences and words, for each of these words, find the number of its occurences in all the sentences.
We define a word as a non-empty maximum sequence of characters that can contain only lowercase letters, uppercase letters, digits and underscores '_' (ASCII value 95). Maximum sequence means that the word has to be immediately preceded by a character not allowed to occur in a word or by the left boundary of the sentence, and it has to be immediately followed by a character not allowed to occur in a word or by the right boundary of the sentence.
Given sentences and words, for each of these words, find the number of its occurrences in all the sentences.
In the first line there is a single integer . Each of the next lines contains a single sentence. After that, in the next line, there is a single integer denoting the number of words. In the i-th of the next lines, there is a single word denoting the i-th word for which, you have to find the number of its occurrences in the sentences.
For every word, print the number of occurrences of the word in all the N sentences listed.
1 foo bar (foo) bar foo-bar foo_bar foo'bar bar-foo bar, foo. 1 foo
- foo is the first word
- (foo) is preceeded by '(' and followed by ')', so it's the second word.
- foo-bar is considered as two words and 'foo' is the first of them. It is preceeded by a space and followed by a hyphen '-'
- bar-foo also contains foo for the same reason mentioned above
- foo_bar is a single single word and hence foo in it is not counted
- foo'bar is considered as two words and 'foo' is the first of them. It is preceeded by a space and followed by a apostrophe "'"
- foo. as it is preceeded by a space and followed by a dot'.'
Solution in Python
import re sentences=  words=  no_of_sentences = int(input("")) for i in range(no_of_sentences): sentences.append(input()) no_of_words = int(input("")) for i in range(no_of_words): words.append(input()) for word in words: print(re.findall("\w+", "\n".join(sentences)).count(word))