UK and US: Part 2

  • + 0 comments

    Java 15:

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = Integer.parseInt(scanner.nextLine());
            
            //Merge all input together
            String input="";
            for(int i=0;i<n;i++)
                input=input+scanner.nextLine()+" ";
            
            int t = Integer.parseInt(scanner.nextLine());
            for(int i=0;i<t;i++)
            {   String uk_word = scanner.nextLine();
                String us_word = uk_word.replaceAll("our","or");
                String regex = String.format("\\b(%s|%s)\\b",uk_word,us_word);
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(input);
                int count=0;
                while(matcher.find())   count++;
                System.out.println(count);
            }
        }
    }