Skip to content

Commit 2a805c8

Browse files
author
Mrinal Chauhan
committed
Longest Common Prefix
1 parent 04518b3 commit 2a805c8

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
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
42
// geeksforgeeks explaination: https://www.geeksforgeeks.org/longest-common-prefix-using-sorting/
53
/* 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:
64
"flower"
@@ -13,8 +11,16 @@
1311
Character Comparison: Compare the characters of the first and last strings until a mismatch is found, tracking the index of the last matching character.
1412
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.
1513
*/
14+
package com.thealgorithms.strings;
15+
import java.util.Arrays; // Specific import
16+
17+
// To find the longest Common Prefix of String array
1618
public class LongestCommonPrefix {
1719

20+
// Private constructor to prevent instantiation of utility class
21+
private LongestCommonPrefix() {
22+
}
23+
1824
// Method to find the longest common prefix
1925
public static String longestPrefix(String[] str) {
2026
int n = str.length;
@@ -43,31 +49,27 @@ public static String longestPrefix(String[] str) {
4349

4450
// Main method to run test cases
4551
public static void main(String[] args) {
46-
// Test Case 1: Normal input
52+
// Test cases
4753
String[] input1 = {"flower", "flow", "flight"};
4854
System.out.println("Test Case 1: " + (longestPrefix(input1).equals("fl") ? "Passed" : "Failed"));
4955

50-
// Test Case 2: No common prefix
5156
String[] input2 = {"dog", "racecar", "car"};
5257
System.out.println("Test Case 2: " + (longestPrefix(input2).equals("") ? "Passed" : "Failed"));
5358

54-
// Test Case 3: Empty array
5559
String[] input3 = {};
5660
System.out.println("Test Case 3: " + (longestPrefix(input3).equals("") ? "Passed" : "Failed"));
5761

58-
// Test Case 4: Single element
5962
String[] input4 = {"alone"};
6063
System.out.println("Test Case 4: " + (longestPrefix(input4).equals("alone") ? "Passed" : "Failed"));
6164

62-
// Test Case 5: Identical strings
6365
String[] input5 = {"same", "same", "same"};
6466
System.out.println("Test Case 5: " + (longestPrefix(input5).equals("same") ? "Passed" : "Failed"));
6567

66-
// Test Case 6: Empty strings
6768
String[] input6 = {"", "", ""};
6869
System.out.println("Test Case 6: " + (longestPrefix(input6).equals("") ? "Passed" : "Failed"));
6970
}
7071
}
72+
7173
/*
7274
Time and Space Complexity:
7375
Time Complexity:O(n log n + m)

0 commit comments

Comments
 (0)