-
Notifications
You must be signed in to change notification settings - Fork 853
Expand file tree
/
Copy pathLongestCommonPrefix.java
More file actions
45 lines (34 loc) · 1.17 KB
/
LongestCommonPrefix.java
File metadata and controls
45 lines (34 loc) · 1.17 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
LongestCommonPrefix
import java.util.Scanner;
import java.util.Arrays;
public class LongestCommonPrefix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking user input for number of strings
System.out.print("Enter number of strings: ");
int n = sc.nextInt();
sc.nextLine(); // consume newline
String[] strs = new String[n];
System.out.println("Enter the strings one by one:");
for (int i = 0; i < n; i++) {
strs[i] = sc.nextLine();
}
// Call the function
String result = longestCommonPrefix(strs);
System.out.println("Longest Common Prefix: " + result);
sc.close();
}
public static String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
Arrays.sort(strs); // Sort the array
String first = strs[0];
String last = strs[strs.length - 1];
int i = 0;
while (i < first.length() && i < last.length() && first.charAt(i) == last.charAt(i)) {
i++;
}
return first.substring(0, i);
}
}