We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
$_fp=fopen("php://stdin","r");$numSentences=(int)fgets(STDIN);$sentences=array();for($i=0;$i<$numSentences;$i++){$sentences[]=trim(fgets(STDIN));}$numWords=(int)fgets(STDIN);$words=array();for($i=0;$i<$numWords;$i++){$words[]=trim(fgets(STDIN));}// Count occurrences of each word in all sentences$results=array();foreach($wordsas$word){$results[$word]=0;foreach($sentencesas$sentence){$pattern='/(?<![a-zA-Z0-9_])('.$word.')(?![a-zA-Z0-9_])/';$numMatches=preg_match_all($pattern,$sentence,$matches);$results[$word]+=$numMatches;}}// Print resultsforeach($resultsas$word=>$count){echo$count."\n";}fclose($_fp);
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find a Word
You are viewing a single comment's thread. Return to all comments →
PHP solution: