Skip to content

Commit f2d4d66

Browse files
committed
Added new solution
1 parent 9ec1534 commit f2d4d66

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public String longestCommonPrefix(String[] strs) {
3+
String prefix = "";
4+
int length = strs[0].length();
5+
for (int i = 1; i < strs.length; i++) {
6+
length = Math.min(length, strs[i].length());
7+
}
8+
for (int j = 0; j < length; j++) {
9+
char sol = strs[0].charAt(j);
10+
for (int i = 1; i < strs.length; i++) {
11+
if (sol != strs[i].charAt(j)) return prefix;
12+
}
13+
prefix += sol;
14+
}
15+
return prefix;
16+
}
17+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Here is a list of the problems I've solved along with links to the corresponding
4040
| [Min Stack](https://leetcode.com/problems/min-stack/) | [Java](Java/src/main/java/problems/leetcode/stack/MinStack.java) |
4141
| [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Java](Java/src/main/java/problems/leetcode/stack/ReversePolish.java) |
4242
| [3Sum](https://leetcode.com/problems/3sum/) | [Java](Java/src/main/java/problems/leetcode/two_pointers/ThreeSum.java) |
43+
| [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix) | [Java](Java/src/main/java/problems/leetcode/others/LongestCommonPrefix.java) |
4344

4445
## Java Solutions
4546

0 commit comments

Comments
 (0)