-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefix.java
More file actions
29 lines (22 loc) · 872 Bytes
/
Prefix.java
File metadata and controls
29 lines (22 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.*;
public class Prefix {
public static String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
String prefix = strs[0];
for (int i = 1; i < strs.length; i++) {
// Reduce prefix until current string starts with it
while (!strs[i].startsWith(prefix)) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) return "";
}
}
return prefix;
}
public static void main(String[] args) {
// Example Input
String[] strs1 = {"flower", "flow", "flight"};
String[] strs2 = {"dog", "racecar", "car"};
System.out.println("Output 1: " + longestCommonPrefix(strs1)); // fl
System.out.println("Output 2: " + longestCommonPrefix(strs2)); // ""
}
}