Skip to content

Commit

Permalink
Added new solution
Browse files Browse the repository at this point in the history
  • Loading branch information
PalmaAnd committed Jul 8, 2024
1 parent 9ec1534 commit f2d4d66
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public String longestCommonPrefix(String[] strs) {
String prefix = "";
int length = strs[0].length();
for (int i = 1; i < strs.length; i++) {
length = Math.min(length, strs[i].length());
}
for (int j = 0; j < length; j++) {
char sol = strs[0].charAt(j);
for (int i = 1; i < strs.length; i++) {
if (sol != strs[i].charAt(j)) return prefix;
}
prefix += sol;
}
return prefix;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Here is a list of the problems I've solved along with links to the corresponding
| [Min Stack](https://leetcode.com/problems/min-stack/) | [Java](Java/src/main/java/problems/leetcode/stack/MinStack.java) |
| [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Java](Java/src/main/java/problems/leetcode/stack/ReversePolish.java) |
| [3Sum](https://leetcode.com/problems/3sum/) | [Java](Java/src/main/java/problems/leetcode/two_pointers/ThreeSum.java) |
| [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix) | [Java](Java/src/main/java/problems/leetcode/others/LongestCommonPrefix.java) |

## Java Solutions

Expand Down

0 comments on commit f2d4d66

Please sign in to comment.