Skip to content

Commit 331b954

Browse files
author
Mrinal Chauhan
committed
Longest Common Prefix
1 parent 4df41d6 commit 331b954

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// To find the longest Common Prefix in String array
21
// geeksforgeeks explaination: https://www.geeksforgeeks.org/longest-common-prefix-using-sorting/
32
/* 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:
43
"flower"
@@ -12,8 +11,10 @@
1211
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.
1312
*/
1413
package com.thealgorithms.strings;
15-
import java.util.Arrays; // Specific import
16-
// To find the longest Common Prefix of String array
14+
import org.junit.Test;
15+
import static org.junit.Assert.assertEquals;
16+
import java.util.Arrays;
17+
1718
public final class LongestCommonPrefix {
1819
// Private constructor to prevent instantiation of utility class
1920
private LongestCommonPrefix() {
@@ -24,15 +25,12 @@ public static String longestPrefix(String[] str) {
2425
if (n == 0) {
2526
return "";
2627
}
27-
2828
// Sort the array to bring similar prefixes closer
2929
Arrays.sort(str);
30-
3130
// Compare the first and last strings after sorting
3231
String first = str[0];
3332
String last = str[n - 1];
3433
int len = Math.min(first.length(), last.length());
35-
3634
// Find the longest common prefix
3735
int i;
3836
for (i = 0; i < len; i++) {
@@ -42,30 +40,34 @@ public static String longestPrefix(String[] str) {
4240
}
4341
return first.substring(0, i);
4442
}
45-
46-
// Main method to run test cases
47-
public static void main(String[] args) {
48-
// Test cases
49-
String[] input1 = {"flower", "flow", "flight"};
50-
System.out.println("Test Case 1: " + (longestPrefix(input1).equals("fl") ? "Passed" : "Failed"));
43+
// JUnit Test cases
44+
@Test
45+
public void testLongestPrefix() {
46+
// Test case 1
47+
String[] input1 = { "flower", "flow", "flight" };
48+
assertEquals("fl", longestPrefix(input1));
5149

52-
String[] input2 = {"dog", "racecar", "car"};
53-
System.out.println("Test Case 2: " + (longestPrefix(input2).equals("") ? "Passed" : "Failed"));
50+
// Test case 2
51+
String[] input2 = { "dog", "racecar", "car" };
52+
assertEquals("", longestPrefix(input2));
5453

54+
// Test case 3
5555
String[] input3 = {};
56-
System.out.println("Test Case 3: " + (longestPrefix(input3).equals("") ? "Passed" : "Failed"));
56+
assertEquals("", longestPrefix(input3));
5757

58-
String[] input4 = {"alone"};
59-
System.out.println("Test Case 4: " + (longestPrefix(input4).equals("alone") ? "Passed" : "Failed"));
58+
// Test case 4
59+
String[] input4 = { "alone" };
60+
assertEquals("alone", longestPrefix(input4));
6061

61-
String[] input5 = {"same", "same", "same"};
62-
System.out.println("Test Case 5: " + (longestPrefix(input5).equals("same") ? "Passed" : "Failed"));
62+
// Test case 5
63+
String[] input5 = { "same", "same", "same" };
64+
assertEquals("same", longestPrefix(input5));
6365

64-
String[] input6 = {"", "", ""};
65-
System.out.println("Test Case 6: " + (longestPrefix(input6).equals("") ? "Passed" : "Failed"));
66+
// Test case 6
67+
String[] input6 = { "", "", "" };
68+
assertEquals("", longestPrefix(input6));
6669
}
6770
}
68-
6971
/*
7072
Time and Space Complexity:
7173
Time Complexity: O(n log n + m)

0 commit comments

Comments
 (0)