Skip to content

Commit 98f9fe7

Browse files
author
applewjg
committed
Longest CommonPrefix
Change-Id: I0be489b105a37722f5041d40bcfe8f52d5777f32
1 parent b27739d commit 98f9fe7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

LongestCommonPrefix.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 20, 2014
4+
Problem: Longest Common Prefix
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/longest-common-prefix/
7+
Notes:
8+
Write a function to find the longest common prefix string amongst an array of strings.
9+
10+
Solution: ...
11+
*/
12+
public class Solution {
13+
public String longestCommonPrefix(String[] strs) {
14+
if (strs.length == 0) return new String("");
15+
for(int i = 0;i < strs[0].length(); ++i){
16+
for(int j = 1;j < strs.length; ++j){
17+
if(i >= strs[j].length() || strs[j].charAt(i) != strs[0].charAt(i))
18+
return strs[0].substring(0,i);
19+
}
20+
}
21+
return strs[0];
22+
}
23+
}

0 commit comments

Comments
 (0)