UK and US: Part 2
The following is the solution to the Hacker Rank problem “UK and US: Part 2" 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 numberOfLines =
sc.nextInt();
String line ="";
int count=0;
//getting all the lines in String
while(numberOfLines--
>=0)
{
line+=(" "+sc.nextLine());
}
int
numberOfTestCases=sc.nextInt();
//processing test cases one by one
while(numberOfTestCases-- >=0)
{
String americanword
=sc.nextLine();
count=0;
Pattern ra = Pattern.compile("\\b"+americanword+"\\b");
String pattern =
americanword.replace("our","or");
if(!pattern.equals(""))
{
Pattern rb = Pattern.compile("\\b"+pattern+"\\b");
// Now create matcher object.
Matcher ma = ra.matcher(line);
Matcher mb = rb.matcher(line);
while(ma.find())
{
count++;
}
while(mb.find())
{
count++;
}
System.out.println(count);
}
}
}
}
|
No comments:
Post a Comment