• + 3 comments

    i solved this probelm by using findall and regular expression..

    reg = r(?<=[\W]) + searching_word + (?=[\W])
    :: \W represents non unicode characters... in this problem, playing a role of wrapper...
    

    for ignoring '_'(underscore) and taking first and last word into consideration more conveniently, i made some tricks..

    pre-processing::

    1) deleting all the '_'s..
    :: foo_bar --> foobar
    
    2) putting '*' in first and last position..
    :: foo foo foo -> *foo foo foo*
    

    code ::

    import re
    
    n = int(input())
    st = '\n'.join(['*'+input().strip().replace('_','')+'*' for _ in range(n)])
    for _ in range(int(input())):
        wd = input().strip()
        reg = r'(?<=[\W])'+ wd +'(?=[\W])'
        m = re.findall(reg,st)
        print(len(m))