You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* 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:
4
3
"flower"
@@ -12,8 +11,10 @@
12
11
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.
13
12
*/
14
13
packagecom.thealgorithms.strings;
15
-
importjava.util.Arrays; // Specific import
16
-
// To find the longest Common Prefix of String array
14
+
importorg.junit.Test;
15
+
importstaticorg.junit.Assert.assertEquals;
16
+
importjava.util.Arrays;
17
+
17
18
publicfinalclassLongestCommonPrefix {
18
19
// Private constructor to prevent instantiation of utility class
19
20
privateLongestCommonPrefix() {
@@ -24,15 +25,12 @@ public static String longestPrefix(String[] str) {
24
25
if (n == 0) {
25
26
return"";
26
27
}
27
-
28
28
// Sort the array to bring similar prefixes closer
29
29
Arrays.sort(str);
30
-
31
30
// Compare the first and last strings after sorting
32
31
Stringfirst = str[0];
33
32
Stringlast = str[n - 1];
34
33
intlen = Math.min(first.length(), last.length());
35
-
36
34
// Find the longest common prefix
37
35
inti;
38
36
for (i = 0; i < len; i++) {
@@ -42,30 +40,34 @@ public static String longestPrefix(String[] str) {
42
40
}
43
41
returnfirst.substring(0, i);
44
42
}
45
-
46
-
// Main method to run test cases
47
-
publicstaticvoidmain(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
+
publicvoidtestLongestPrefix() {
46
+
// Test case 1
47
+
String[] input1 = {"flower", "flow", "flight"};
48
+
assertEquals("fl", longestPrefix(input1));
51
49
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));
54
53
54
+
// Test case 3
55
55
String[] input3 = {};
56
-
System.out.println("Test Case 3: " + (longestPrefix(input3).equals("") ? "Passed" : "Failed"));
56
+
assertEquals("", longestPrefix(input3));
57
57
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));
60
61
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));
63
65
64
-
String[] input6 = {"", "", ""};
65
-
System.out.println("Test Case 6: " + (longestPrefix(input6).equals("") ? "Passed" : "Failed"));
0 commit comments