-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongPref.java
More file actions
33 lines (25 loc) · 749 Bytes
/
LongPref.java
File metadata and controls
33 lines (25 loc) · 749 Bytes
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
import java.util.*;
class LongPref {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String[] strs = new String[n];
for (int i = 0; i < n; i++) {
strs[i] = sc.nextLine();
}
String pref = strs[0];
int k = pref.length();
while (k > 0) {
String target = pref.substring(0, k);
boolean allMatch = Arrays.stream(strs)
.allMatch(s -> s.startsWith(target));
if (allMatch) {
System.out.print(target);
return;
}
k--;
}
System.out.print("");
}
}