We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 57a8c30 commit 504b8c8Copy full SHA for 504b8c8
src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java
@@ -4,15 +4,18 @@
4
5
public final class LongestCommonPrefix {
6
public String longestCommonPrefix(String[] strs) {
7
- if(strs == null || strs.length == 0) return "";
+ if (strs == null || strs.length == 0) {
8
+ return "";
9
+ }
10
11
Arrays.sort(strs);
12
String shortest = strs[0];
13
String longest = strs[strs.length - 1];
14
15
int index = 0;
- while(index < shortest.length() && index < longest.length() && shortest.charAt(index) == longest.charAt(index))
16
+ while (index < shortest.length() && index < longest.length() && shortest.charAt(index) == longest.charAt(index)) {
17
index++;
18
19
20
return shortest.substring(0, index);
21
}
0 commit comments