Find A Word
The following is the solution to Hacker Rank problem "Find A Word" using Java. For other Hacker Rank problem solutions visit my Hacker Rank Solutions Page.
/**
* @author Arun.G
*
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[]
args) {
/* Enter your code here. Read input from
STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int no = sc.nextInt();
String line="";
while(no-- >=0)
line+=" "+sc.nextLine();
//split all the words using the regex it
considers only words with _ as single word
String[] AllWords = line.split("\\W");
int
testCases=sc.nextInt();
while(testCases-->=0)
{
String word = sc.nextLine();
int count=0;
if(!word.equals(""))
{
String pattern=word;
//See if we have a match and increase
count
for(int i=0;i<AllWords.length;i++)
{
if(AllWords[i].matches(pattern))
count++;
}
System.out.println(count);
}
}
}
}
|
No comments:
Post a Comment