Skip to content

Commit 0b60c1f

Browse files
committed
Create UK and US: Part 2
1 parent 2e311c4 commit 0b60c1f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

UK and US: Part 2

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.Scanner;
2+
import java.util.regex.Matcher;
3+
import java.util.regex.Pattern;
4+
5+
public class Solution{
6+
7+
8+
public static void main(String[] args) {
9+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
10+
11+
Scanner in = new Scanner(System.in);
12+
int n = Integer.parseInt(in.nextLine());
13+
String[] stringArray = new String[n];
14+
for (int i = 0; i < n; i++)
15+
stringArray[i] = in.nextLine();
16+
17+
int t = Integer.parseInt(in.nextLine());
18+
String ch;
19+
for (int i = 0; i < t; i++) {
20+
ch = in.nextLine();
21+
String stringUS = null;
22+
if(ch.matches("([\\w]+)our.*"))
23+
stringUS = ch.replace("our", "or");
24+
char[] chArray = ch.toCharArray();
25+
char[] chArrayUS = stringUS.toCharArray();
26+
StringBuffer strbuff = new StringBuffer();
27+
StringBuffer strbuff2 = new StringBuffer();
28+
29+
strbuff.append(ch + "((\\W)+|$)");
30+
strbuff2.append(stringUS + "((\\W)+|$)");
31+
32+
String regexUK = strbuff.toString();
33+
String regexUS = strbuff2.toString();
34+
35+
Pattern patternUK = Pattern.compile(regexUK);
36+
Pattern patternUS = Pattern.compile(regexUS);
37+
38+
int counter = 0;
39+
for (int j = 0; j < n; j++) {
40+
Matcher matcherUK = patternUK.matcher(stringArray[j]);
41+
Matcher matcherUS = patternUS.matcher(stringArray[j]);
42+
43+
44+
while(matcherUK.find())
45+
counter++;
46+
while(matcherUS.find())
47+
counter++;
48+
}
49+
System.out.println(counter);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)