-
Notifications
You must be signed in to change notification settings - Fork 4
/
Find A Substring
40 lines (33 loc) · 1.08 KB
/
Find A Substring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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 in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String[] stringArray = new String[n];
for (int i = 0; i < n; i++)
stringArray[i] = in.nextLine();
int t = Integer.parseInt(in.nextLine());
String ch;
for (int i = 0; i < t; i++) {
ch = in.nextLine();
char[] chArray = ch.toCharArray();
StringBuffer strbuff = new StringBuffer();
strbuff.append("[0-9A-Za-z|_]");
for (int j = 0; j < chArray.length; j++)
strbuff.append("[" + chArray[j]+"]");
strbuff.append("[0-9A-Za-z|_]");
String regex = strbuff.toString();
Pattern pattern = Pattern.compile(regex);
int counter = 0;
for (int j = 0; j < n; j++) {
Matcher matcher = pattern.matcher(stringArray[j]);
while(matcher.find())
counter++;
}
System.out.println(counter);
}
}
}