|
2 | 2 | import java.util.*; |
3 | 3 | // To find the longest Common Prefix of String array |
4 | 4 | // geeksforgeeks explaination: https://www.geeksforgeeks.org/longest-common-prefix-using-sorting/ |
5 | | - |
6 | 5 | /* The Longest Common Prefix (LCP) of a set of strings is the longest substring that appears at the beginning of each of the strings in the set. For example, given the strings: |
7 | 6 | "flower" |
8 | 7 | "flow" |
9 | 8 | "flight" |
10 | | -The longest common prefix is "fl", as it is the longest substring that is common at the start of all three strings. */ |
11 | | -public class LongestCommonPrefix { |
12 | | - public static String longestPrefix(String[] str){ |
13 | | - int n=str.length; |
14 | | - Arrays.sort(str); |
15 | | - if(n==0){ |
16 | | - return ""; |
17 | | - } |
18 | | -/* |
| 9 | +The longest common prefix is "fl", as it is the longest substring that is common at the start of all three strings. |
| 10 | +Approach:- |
19 | 11 | Sort the Array: Sort the array of strings to bring strings with common prefixes adjacent to each other. |
20 | 12 | Identify Extremes: Select the first and last strings from the sorted array for comparison, as they will have the longest common prefix. |
21 | 13 | Character Comparison: Compare the characters of the first and last strings until a mismatch is found, tracking the index of the last matching character. |
22 | 14 | Return Prefix: Return the substring of the first string from the start to the index of the last matching character, which represents the longest common prefix. |
23 | 15 | */ |
24 | | - String first=str[0]; |
25 | | - String last=str[n-1]; |
26 | | - int len=Math.min(first.length(),last.length()); |
27 | | - int i; |
28 | | - for(i=0;i<len;i++){ |
29 | | - if(first.charAt(i)!=last.charAt(i)){ |
30 | | - break; |
31 | | - } |
32 | | - } |
33 | | - return first.substring(0, i); |
| 16 | +public class LongestCommonPrefix { |
| 17 | + |
| 18 | + // Method to find the longest common prefix |
| 19 | + public static String longestPrefix(String[] str) { |
| 20 | + int n = str.length; |
| 21 | + if (n == 0) { |
| 22 | + return ""; |
| 23 | + } |
| 24 | + |
| 25 | + // Sort the array to bring similar prefixes closer |
| 26 | + Arrays.sort(str); |
| 27 | + |
| 28 | + // Compare the first and last strings after sorting |
| 29 | + String first = str[0]; |
| 30 | + String last = str[n - 1]; |
| 31 | + int len = Math.min(first.length(), last.length()); |
| 32 | + |
| 33 | + // Find the longest common prefix |
| 34 | + int i; |
| 35 | + for (i = 0; i < len; i++) { |
| 36 | + if (first.charAt(i) != last.charAt(i)) { |
| 37 | + break; |
| 38 | + } |
| 39 | + } |
34 | 40 |
|
| 41 | + return first.substring(0, i); |
35 | 42 | } |
36 | | - public static void main(String args[]){ |
37 | | - //Input test case |
38 | | - String[] arr = {"flower", "flow", "flight"}; |
39 | | - |
40 | | - // Output the result |
41 | | - System.out.println("Longest Common Prefix: " + longestPrefix(arr)); //flo |
| 43 | + |
| 44 | + // Main method to run test cases |
| 45 | + public static void main(String[] args) { |
| 46 | + // Test Case 1: Normal input |
| 47 | + String[] input1 = {"flower", "flow", "flight"}; |
| 48 | + System.out.println("Test Case 1: " + (longestPrefix(input1).equals("fl") ? "Passed" : "Failed")); |
| 49 | + |
| 50 | + // Test Case 2: No common prefix |
| 51 | + String[] input2 = {"dog", "racecar", "car"}; |
| 52 | + System.out.println("Test Case 2: " + (longestPrefix(input2).equals("") ? "Passed" : "Failed")); |
| 53 | + |
| 54 | + // Test Case 3: Empty array |
| 55 | + String[] input3 = {}; |
| 56 | + System.out.println("Test Case 3: " + (longestPrefix(input3).equals("") ? "Passed" : "Failed")); |
| 57 | + |
| 58 | + // Test Case 4: Single element |
| 59 | + String[] input4 = {"alone"}; |
| 60 | + System.out.println("Test Case 4: " + (longestPrefix(input4).equals("alone") ? "Passed" : "Failed")); |
| 61 | + |
| 62 | + // Test Case 5: Identical strings |
| 63 | + String[] input5 = {"same", "same", "same"}; |
| 64 | + System.out.println("Test Case 5: " + (longestPrefix(input5).equals("same") ? "Passed" : "Failed")); |
| 65 | + |
| 66 | + // Test Case 6: Empty strings |
| 67 | + String[] input6 = {"", "", ""}; |
| 68 | + System.out.println("Test Case 6: " + (longestPrefix(input6).equals("") ? "Passed" : "Failed")); |
42 | 69 | } |
43 | 70 | } |
44 | 71 | /* |
|
0 commit comments