-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution.java
More file actions
28 lines (26 loc) · 824 Bytes
/
Solution.java
File metadata and controls
28 lines (26 loc) · 824 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
package _014;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/04/26
* desc :
* </pre>
*/
public class Solution {
public String longestCommonPrefix(String[] strs) {
int len = strs.length;
if (len == 0) return "";
int minLen = 0x7fffffff;
for (String str : strs) minLen = Math.min(minLen, str.length());
for (int j = 0; j < minLen; ++j)
for (int i = 1; i < len; ++i)
if (strs[0].charAt(j) != strs[i].charAt(j))
return strs[0].substring(0, j);
return strs[0].substring(0, minLen);
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.longestCommonPrefix(new String[]{"abc", "abcd", "ab"}));
}
}