|
1 |
| -package com.thealgorithms.strings; |
2 |
| -import java.util.*; |
3 |
| -// To find the longest Common Prefix of String array |
| 1 | +// To find the longest Common Prefix in String array |
4 | 2 | // geeksforgeeks explaination: https://www.geeksforgeeks.org/longest-common-prefix-using-sorting/
|
5 | 3 | /* 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:
|
6 | 4 | "flower"
|
|
13 | 11 | Character Comparison: Compare the characters of the first and last strings until a mismatch is found, tracking the index of the last matching character.
|
14 | 12 | 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.
|
15 | 13 | */
|
| 14 | +package com.thealgorithms.strings; |
| 15 | +import java.util.Arrays; // Specific import |
| 16 | + |
| 17 | +// To find the longest Common Prefix of String array |
16 | 18 | public class LongestCommonPrefix {
|
17 | 19 |
|
| 20 | + // Private constructor to prevent instantiation of utility class |
| 21 | + private LongestCommonPrefix() { |
| 22 | + } |
| 23 | + |
18 | 24 | // Method to find the longest common prefix
|
19 | 25 | public static String longestPrefix(String[] str) {
|
20 | 26 | int n = str.length;
|
@@ -43,31 +49,27 @@ public static String longestPrefix(String[] str) {
|
43 | 49 |
|
44 | 50 | // Main method to run test cases
|
45 | 51 | public static void main(String[] args) {
|
46 |
| - // Test Case 1: Normal input |
| 52 | + // Test cases |
47 | 53 | String[] input1 = {"flower", "flow", "flight"};
|
48 | 54 | System.out.println("Test Case 1: " + (longestPrefix(input1).equals("fl") ? "Passed" : "Failed"));
|
49 | 55 |
|
50 |
| - // Test Case 2: No common prefix |
51 | 56 | String[] input2 = {"dog", "racecar", "car"};
|
52 | 57 | System.out.println("Test Case 2: " + (longestPrefix(input2).equals("") ? "Passed" : "Failed"));
|
53 | 58 |
|
54 |
| - // Test Case 3: Empty array |
55 | 59 | String[] input3 = {};
|
56 | 60 | System.out.println("Test Case 3: " + (longestPrefix(input3).equals("") ? "Passed" : "Failed"));
|
57 | 61 |
|
58 |
| - // Test Case 4: Single element |
59 | 62 | String[] input4 = {"alone"};
|
60 | 63 | System.out.println("Test Case 4: " + (longestPrefix(input4).equals("alone") ? "Passed" : "Failed"));
|
61 | 64 |
|
62 |
| - // Test Case 5: Identical strings |
63 | 65 | String[] input5 = {"same", "same", "same"};
|
64 | 66 | System.out.println("Test Case 5: " + (longestPrefix(input5).equals("same") ? "Passed" : "Failed"));
|
65 | 67 |
|
66 |
| - // Test Case 6: Empty strings |
67 | 68 | String[] input6 = {"", "", ""};
|
68 | 69 | System.out.println("Test Case 6: " + (longestPrefix(input6).equals("") ? "Passed" : "Failed"));
|
69 | 70 | }
|
70 | 71 | }
|
| 72 | + |
71 | 73 | /*
|
72 | 74 | Time and Space Complexity:
|
73 | 75 | Time Complexity:O(n log n + m)
|
|
0 commit comments