Find A Sub-Word

  • + 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());
            String array[]=new String[n];
            for (int i=0;i<n;i++)   array[i]=scanner.nextLine();
            int q = Integer.parseInt(scanner.nextLine());
            for (int i=0;i<q;i++)
            {   String s = scanner.nextLine();
                String regex = String.format("\\w%s\\w", s);
                Pattern pattern = Pattern.compile(regex);  
                int count=0;
                for(int j=0;j<n;j++)
                {   Matcher matcher = pattern.matcher(array[j]);
                    while(matcher.find())   count++;
                }
                System.out.println(count);
            }
        }
    }