# [LeetCode] Longest Common Prefix

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

Subscribe to see which companies asked this question

public class Solution {
public String longestCommonPrefix(String[] strs) {
int n = strs.length;
if (n == 0)
return "";

String str = strs[0];
int len = str.length(), i = 0;
for ( ; i < len; i++) {
for (int j = 1; j < n; j++) {
if (strs[j].length() < i + 1)
return str.substring(0, i);
if (str.charAt(i) != strs[j].charAt(i))
return str.substring(0, i);
}
}
return str;
}
}

#### LeetCode 14 Longest Common Prefix（最长公共前缀）（String）

2015-10-16 22:05:46

#### 关于LeetCode中Longest Common Prefix一题的理解

2016-09-04 18:33:06

#### [LeetCode][14]Longest Common Prefix解析 两种算法和底层源码的深入对比-Java实现

2016-06-15 13:12:37

#### [LeetCode]Longest Common Prefix

2013-11-22 12:37:41

#### LeetCode 14 Longest Common Prefix (C,C++,Java,Python)

2015-05-08 18:40:35

#### [leetcode:python]14.Longest Common Prefix

2017-05-10 19:07:20

#### [LeetCode] 014. Longest Common Prefix (Easy) (C++/Java/Python)

2015-03-03 15:34:34

#### LeetCode 14 — Longest Common Prefix（C++ Java Python）

2014-04-03 18:42:35

#### LeetCode_Easy心得：14. Longest Common Prefix（C语言）

2017-07-22 12:05:38

#### LeetCode 14 Longest Common Prefix C语言实现

2017-03-27 10:49:05