MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0014/Solution.java:11–28  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/04/26 desc :

Source from the content-addressed store, hash-verified

9 * </pre>
10 */
11public class Solution {
12 public String longestCommonPrefix(String[] strs) {
13 int len = strs.length;
14 if (len == 0) return "";
15 int minLen = 0x7fffffff;
16 for (String str : strs) minLen = Math.min(minLen, str.length());
17 for (int j = 0; j < minLen; ++j)
18 for (int i = 1; i < len; ++i)
19 if (strs[0].charAt(j) != strs[i].charAt(j))
20 return strs[0].substring(0, j);
21 return strs[0].substring(0, minLen);
22 }
23
24 public static void main(String[] args) {
25 Solution solution = new Solution();
26 System.out.println(solution.longestCommonPrefix(new String[]{"abc", "abcd", "ab"}));
27 }
28}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected