Find
A Substring
The following is the solution to the Hacker Rank problem “Find
A Substring" using Java.
/**
* @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();
int
testCases=sc.nextInt();
while(testCases-->=0)
{
String word = sc.nextLine();
int count=0;
if(!word.equals(""))
{
String pattern ="[a-z_]{1,}"+word+"[a-z_]{1,}";
Pattern wordPattern =
Pattern.compile(pattern);
Matcher m =
wordPattern.matcher(line);
while(m.find())
count++;
System.out.println(count);
}
}
}
}
|
No comments:
Post a Comment