You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank - Regex Find a Word
Solution

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.

Beeze Aal
Beeze Aal

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.

Input Format

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.

Constraints

1<=N<=100
1<=T<=10

Output format

For every word, print the number of occurrences of the word in all the N sentences listed.

Sample Input

1
foo bar (foo) bar foo-bar foo_bar foo'bar bar-foo bar, foo.
1
foo

Sample Output

6

Explanation

  • 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))