A Sub word is defined as follows:
- A sequence of word characters (i.e., English alphabetic letters, digits, and/or underscores) that occur in the same exact order (i.e., as a contiguous sequence) inside another word.
- It is preceded and succeeded by word characters only.
Given sentences consisting of one or more words separated by non-word characters, process queries where each query consists of a single string, . To process each query, count the number of occurrences of as a sub-word in all sentences, then print the number of occurrences on a new line.
Sample input: 
1
existing pessimist optimist this is
1
is
Sample output
3
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<String> s = new ArrayList();
for (int i = 0; i <= n; i++)
{
    String st = sc.nextLine();
    s.add(st);
}
int q = sc.nextInt();
for (int i = 0; i<q;i++)
{
    int count = 0;
    String st = sc.nextLine();
    String check = "\\w"+st+"\\w";
    Pattern p = Pattern.compile(check);
    for (int j = 0; j < n; j++)
    {
       Matcher m = p.matcher(s.get(j));
       while (m.find())
       {
           count += 1;
       }
   }
   System.out.println(count);
}
Can someone help me figure out why the above code gives wrong answer for the problem?
 
     
    