From 29c50d399159af4894d2a71b3abc7b7b328939c3 Mon Sep 17 00:00:00 2001 From: Mrinal Chauhan Date: Tue, 22 Oct 2024 02:02:13 +0530 Subject: [PATCH 1/8] Longest Common Prefix --- .../strings/longestCommonPrefix.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/longestCommonPrefix.java diff --git a/src/main/java/com/thealgorithms/strings/longestCommonPrefix.java b/src/main/java/com/thealgorithms/strings/longestCommonPrefix.java new file mode 100644 index 000000000000..a34969f71f29 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/longestCommonPrefix.java @@ -0,0 +1,51 @@ +package com.thealgorithms.strings; +import java.util.*; +//To find the longest Common Prefix of String array +public class longestCommonPrefix { + public static String longestPrefix(String[] str){ + int n=str.length; + Arrays.sort(str); + if(n==0){ + return ""; + } + String first=str[0]; + String last=str[n-1]; + int len=Math.min(first.length(),last.length()); + int i; + for(i=0;i Date: Tue, 22 Oct 2024 02:18:26 +0530 Subject: [PATCH 2/8] Longest Common Prefix --- .../{longestCommonPrefix.java => LongestCommonPrefix.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/com/thealgorithms/strings/{longestCommonPrefix.java => LongestCommonPrefix.java} (100%) diff --git a/src/main/java/com/thealgorithms/strings/longestCommonPrefix.java b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java similarity index 100% rename from src/main/java/com/thealgorithms/strings/longestCommonPrefix.java rename to src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java From 06137e3dd215c0d4b5674d9051f71eddd7d6254a Mon Sep 17 00:00:00 2001 From: Mrinal Chauhan Date: Tue, 22 Oct 2024 02:31:24 +0530 Subject: [PATCH 3/8] Longest Common Prefix --- .../thealgorithms/strings/LongestCommonPrefix.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java index a34969f71f29..6e476a8e3cdf 100644 --- a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java +++ b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java @@ -1,7 +1,14 @@ package com.thealgorithms.strings; import java.util.*; -//To find the longest Common Prefix of String array -public class longestCommonPrefix { +// To find the longest Common Prefix of String array +// geeksforgeeks explaination: https://www.geeksforgeeks.org/longest-common-prefix-using-sorting/ + +/* 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: +"flower" +"flow" +"flight" +The longest common prefix is "fl", as it is the longest substring that is common at the start of all three strings. */ +public class LongestCommonPrefix { public static String longestPrefix(String[] str){ int n=str.length; Arrays.sort(str); From 2dec365c9110c1be318bc580159d12d535080801 Mon Sep 17 00:00:00 2001 From: Mrinal Chauhan Date: Tue, 22 Oct 2024 02:36:04 +0530 Subject: [PATCH 4/8] Longest Common Prefix --- .../java/com/thealgorithms/strings/LongestCommonPrefix.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java index 6e476a8e3cdf..59fce101af84 100644 --- a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java +++ b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java @@ -15,6 +15,12 @@ public static String longestPrefix(String[] str){ if(n==0){ return ""; } +/* +Sort the Array: Sort the array of strings to bring strings with common prefixes adjacent to each other. +Identify Extremes: Select the first and last strings from the sorted array for comparison, as they will have the longest common prefix. +Character Comparison: Compare the characters of the first and last strings until a mismatch is found, tracking the index of the last matching character. +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. +*/ String first=str[0]; String last=str[n-1]; int len=Math.min(first.length(),last.length()); From 04518b36f0fbab23f2503f41e3aac0a66623f210 Mon Sep 17 00:00:00 2001 From: Mrinal Chauhan Date: Tue, 22 Oct 2024 12:59:46 +0530 Subject: [PATCH 5/8] Longest Common Prefix included test cases --- .../strings/LongestCommonPrefix.java | 79 +++++++++++++------ 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java index 59fce101af84..1672af9f3e91 100644 --- a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java +++ b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java @@ -2,43 +2,70 @@ import java.util.*; // To find the longest Common Prefix of String array // geeksforgeeks explaination: https://www.geeksforgeeks.org/longest-common-prefix-using-sorting/ - /* 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: "flower" "flow" "flight" -The longest common prefix is "fl", as it is the longest substring that is common at the start of all three strings. */ -public class LongestCommonPrefix { - public static String longestPrefix(String[] str){ - int n=str.length; - Arrays.sort(str); - if(n==0){ - return ""; - } -/* +The longest common prefix is "fl", as it is the longest substring that is common at the start of all three strings. +Approach:- Sort the Array: Sort the array of strings to bring strings with common prefixes adjacent to each other. Identify Extremes: Select the first and last strings from the sorted array for comparison, as they will have the longest common prefix. Character Comparison: Compare the characters of the first and last strings until a mismatch is found, tracking the index of the last matching character. 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. */ - String first=str[0]; - String last=str[n-1]; - int len=Math.min(first.length(),last.length()); - int i; - for(i=0;i Date: Tue, 22 Oct 2024 14:20:24 +0530 Subject: [PATCH 6/8] Longest Common Prefix --- .../strings/LongestCommonPrefix.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java index 1672af9f3e91..d6eb98bafb52 100644 --- a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java +++ b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java @@ -1,6 +1,4 @@ -package com.thealgorithms.strings; -import java.util.*; -// To find the longest Common Prefix of String array +// To find the longest Common Prefix in String array // geeksforgeeks explaination: https://www.geeksforgeeks.org/longest-common-prefix-using-sorting/ /* 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: "flower" @@ -13,8 +11,16 @@ Character Comparison: Compare the characters of the first and last strings until a mismatch is found, tracking the index of the last matching character. 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. */ +package com.thealgorithms.strings; +import java.util.Arrays; // Specific import + +// To find the longest Common Prefix of String array public class LongestCommonPrefix { + // Private constructor to prevent instantiation of utility class + private LongestCommonPrefix() { + } + // Method to find the longest common prefix public static String longestPrefix(String[] str) { int n = str.length; @@ -43,31 +49,27 @@ public static String longestPrefix(String[] str) { // Main method to run test cases public static void main(String[] args) { - // Test Case 1: Normal input + // Test cases String[] input1 = {"flower", "flow", "flight"}; System.out.println("Test Case 1: " + (longestPrefix(input1).equals("fl") ? "Passed" : "Failed")); - // Test Case 2: No common prefix String[] input2 = {"dog", "racecar", "car"}; System.out.println("Test Case 2: " + (longestPrefix(input2).equals("") ? "Passed" : "Failed")); - // Test Case 3: Empty array String[] input3 = {}; System.out.println("Test Case 3: " + (longestPrefix(input3).equals("") ? "Passed" : "Failed")); - // Test Case 4: Single element String[] input4 = {"alone"}; System.out.println("Test Case 4: " + (longestPrefix(input4).equals("alone") ? "Passed" : "Failed")); - // Test Case 5: Identical strings String[] input5 = {"same", "same", "same"}; System.out.println("Test Case 5: " + (longestPrefix(input5).equals("same") ? "Passed" : "Failed")); - // Test Case 6: Empty strings String[] input6 = {"", "", ""}; System.out.println("Test Case 6: " + (longestPrefix(input6).equals("") ? "Passed" : "Failed")); } } + /* Time and Space Complexity: Time Complexity:O(n log n + m) From 4df41d6aaabf4fdeb94012d5a197668108ed761a Mon Sep 17 00:00:00 2001 From: Mrinal Chauhan Date: Tue, 22 Oct 2024 14:48:17 +0530 Subject: [PATCH 7/8] Longest Common Prefix --- .../strings/LongestCommonPrefix.java | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java index d6eb98bafb52..4586d29330ad 100644 --- a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java +++ b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java @@ -13,14 +13,11 @@ */ package com.thealgorithms.strings; import java.util.Arrays; // Specific import - // To find the longest Common Prefix of String array -public class LongestCommonPrefix { - +public final class LongestCommonPrefix { // Private constructor to prevent instantiation of utility class private LongestCommonPrefix() { } - // Method to find the longest common prefix public static String longestPrefix(String[] str) { int n = str.length; @@ -43,10 +40,9 @@ public static String longestPrefix(String[] str) { break; } } - return first.substring(0, i); } - + // Main method to run test cases public static void main(String[] args) { // Test cases @@ -72,22 +68,13 @@ public static void main(String[] args) { /* Time and Space Complexity: -Time Complexity:O(n log n + m) - -Sorting the array takes 𝑂(𝑛 log 𝑛) -O(nlogn), where n is the number of strings. -Comparing the first and last string takes 𝑂(𝑚) -O(m), where m is the length of the shortest string. -Overall, the time complexity is -𝑂(log 𝑛 + 𝑚 ) - - -Space Complexity:O(n) - -Sorting requires 𝑂(𝑛) -O(n) space for the array. -The space complexity for storing the prefix result is -𝑂(1) -O(1) since it depends on the length of the prefix, which is part of the input. -Therefore, the space complexity is 𝑂(𝑛) -*/ \ No newline at end of file +Time Complexity: O(n log n + m) +- Sorting the array takes O(n log n), where n is the number of strings. +- Comparing the first and last string takes O(m), where m is the length of the shortest string. +- Overall, the time complexity is O(n log n + m). + +Space Complexity: O(n) +- Sorting requires O(n) space for the array. +- The space complexity for storing the prefix result is O(1), since it depends on the length of the prefix, which is part of the input. +Therefore, the space complexity is O(n). +*/ From 331b954bdf932e0fef52c4020b467aecc1617754 Mon Sep 17 00:00:00 2001 From: Mrinal Chauhan Date: Wed, 23 Oct 2024 15:28:07 +0530 Subject: [PATCH 8/8] Longest Common Prefix --- .../strings/LongestCommonPrefix.java | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java index 4586d29330ad..857cc045dbae 100644 --- a/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java +++ b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java @@ -1,4 +1,3 @@ -// To find the longest Common Prefix in String array // geeksforgeeks explaination: https://www.geeksforgeeks.org/longest-common-prefix-using-sorting/ /* 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: "flower" @@ -12,8 +11,10 @@ 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. */ package com.thealgorithms.strings; -import java.util.Arrays; // Specific import -// To find the longest Common Prefix of String array +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import java.util.Arrays; + public final class LongestCommonPrefix { // Private constructor to prevent instantiation of utility class private LongestCommonPrefix() { @@ -24,15 +25,12 @@ public static String longestPrefix(String[] str) { if (n == 0) { return ""; } - // Sort the array to bring similar prefixes closer Arrays.sort(str); - // Compare the first and last strings after sorting String first = str[0]; String last = str[n - 1]; int len = Math.min(first.length(), last.length()); - // Find the longest common prefix int i; for (i = 0; i < len; i++) { @@ -42,30 +40,34 @@ public static String longestPrefix(String[] str) { } return first.substring(0, i); } - - // Main method to run test cases - public static void main(String[] args) { - // Test cases - String[] input1 = {"flower", "flow", "flight"}; - System.out.println("Test Case 1: " + (longestPrefix(input1).equals("fl") ? "Passed" : "Failed")); + // JUnit Test cases + @Test + public void testLongestPrefix() { + // Test case 1 + String[] input1 = { "flower", "flow", "flight" }; + assertEquals("fl", longestPrefix(input1)); - String[] input2 = {"dog", "racecar", "car"}; - System.out.println("Test Case 2: " + (longestPrefix(input2).equals("") ? "Passed" : "Failed")); + // Test case 2 + String[] input2 = { "dog", "racecar", "car" }; + assertEquals("", longestPrefix(input2)); + // Test case 3 String[] input3 = {}; - System.out.println("Test Case 3: " + (longestPrefix(input3).equals("") ? "Passed" : "Failed")); + assertEquals("", longestPrefix(input3)); - String[] input4 = {"alone"}; - System.out.println("Test Case 4: " + (longestPrefix(input4).equals("alone") ? "Passed" : "Failed")); + // Test case 4 + String[] input4 = { "alone" }; + assertEquals("alone", longestPrefix(input4)); - String[] input5 = {"same", "same", "same"}; - System.out.println("Test Case 5: " + (longestPrefix(input5).equals("same") ? "Passed" : "Failed")); + // Test case 5 + String[] input5 = { "same", "same", "same" }; + assertEquals("same", longestPrefix(input5)); - String[] input6 = {"", "", ""}; - System.out.println("Test Case 6: " + (longestPrefix(input6).equals("") ? "Passed" : "Failed")); + // Test case 6 + String[] input6 = { "", "", "" }; + assertEquals("", longestPrefix(input6)); } } - /* Time and Space Complexity: Time Complexity: O(n log n + m)