-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm14.java
More file actions
21 lines (19 loc) · 731 Bytes
/
prblm14.java
File metadata and controls
21 lines (19 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class prblm14 {
public static void main(String[] args) {
String[] strs = {"flower","flow","flight"};
System.out.println(new prblm14().longestCommonPrefix(strs));
String[] strs2 = {"dog","racecar","car"};
System.out.println(new prblm14().longestCommonPrefix(strs2));
}
public String longestCommonPrefix(String[] strs) {
if (strs.length == 0) return "";
for (int i = 0; i < strs[0].length(); i++) {
for (int j = 1; j < strs.length; j++) {
if (strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i)) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
}