LeetCode: Longest Common Prefix

LeetCode: Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

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
public class Solution {
public String longestCommonPrefix(String[] strs) {
// handle special case
if(strs.length == 0) return "";

// set the first string as prefix
String prefix = strs[0];

for(int i = 1; i < strs.length; i++) {
int j = 0;
for(; j < prefix.length() && j < strs[i].length(); j++) {
// find the longest prefix for current string
if(prefix.charAt(j) != strs[i].charAt(j)) {
break;
}
}
// update longest prefix
prefix = prefix.substring(0, j);
// This line can be removed, just for performance issue
if(prefix.length() == 0) break;
}

return prefix;
}
}