Skip to content

Commit 04518b3

Browse files
author
Mrinal Chauhan
committed
Longest Common Prefix included test cases
1 parent 2dec365 commit 04518b3

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

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

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,70 @@
22
import java.util.*;
33
// To find the longest Common Prefix of String array
44
// geeksforgeeks explaination: https://www.geeksforgeeks.org/longest-common-prefix-using-sorting/
5-
65
/* 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:
76
"flower"
87
"flow"
98
"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:-
1911
Sort the Array: Sort the array of strings to bring strings with common prefixes adjacent to each other.
2012
Identify Extremes: Select the first and last strings from the sorted array for comparison, as they will have the longest common prefix.
2113
Character Comparison: Compare the characters of the first and last strings until a mismatch is found, tracking the index of the last matching character.
2214
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.
2315
*/
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+
}
3440

41+
return first.substring(0, i);
3542
}
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"));
4269
}
4370
}
4471
/*

0 commit comments

Comments
 (0)