-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay67.java
More file actions
23 lines (22 loc) · 772 Bytes
/
Day67.java
File metadata and controls
23 lines (22 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Day67 {
public static void main(String[] args) {
String[] arr = {"coding", "codezen", "codingninja", "coders"};
String longestCommonPrefix = longestCommonPrefix(arr);
System.out.println("Longest Common Prefix: " + longestCommonPrefix);
}
public static String longestCommonPrefix(String[] arr) {
if (arr == null || arr.length == 0) {
return "";
}
String prefix = arr[0];
for (int i = 1; i < arr.length; i++) {
while (arr[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) {
return "";
}
}
}
return prefix;
}
}