Search This Blog

Sunday, September 1, 2013

Hacker Rank Problem - Regex - The British and American Style of Spelling

The British and American Style of Spelling

The following is the solution to the Hacker Rank problem "The British and American Style of Spelling". 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(americanword);
            String pattern = americanword.replace("z","s");
       
            if(!pattern.equals(""))
            {
            Pattern rb = Pattern.compile(pattern);
          // 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

Labels