-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstRepeatedWord.java
64 lines (54 loc) · 1.67 KB
/
FirstRepeatedWord.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.*;
import java.util.*;
public class FirstRepeatedWord {
static String repeatedWord(String[] ar) {
Set<String> unique = new HashSet<>();
for (int i = 0; i < ar.length; i++) {
if (ar[i].trim().length()>0 && !unique.add(ar[i])) {
return ar[i];
}
}
return "";
}
/**
* 5 1 5 8 12 13
* 5 8 1 23 1 11
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
s= s.replaceAll("\\.", " ");
s= s.replaceAll(",", " ");
s= s.replaceAll("\t", " ");
s= s.replaceAll(":", " ");
s= s.replaceAll(";", " ");
s= s.replaceAll("-", " ");
String[] ar = s.split(" ");
System.out.println(repeatedWord(ar));
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}