Repository: TheAlgorithms/Java
Pull request #5849 (2024-10-15 11:11:53)

Add LongestCommonPrefix.java
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5849?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `0%` with `9 lines` in your changes missing coverage. Please review.
> Project coverage is 66.68%. Comparing base [(`2a518e3`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/2a518e3c9a44c4333c5ec9283895132264773afb?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`5dbe20b`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/5dbe20b9055de93320f122e6af6cac5c8e6ba873?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5849?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...com/thealgorithms/strings/LongestCommonPrefix.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5849?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fstrings%2FLongestCommonPrefix.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zdHJpbmdzL0xvbmdlc3RDb21tb25QcmVmaXguamF2YQ==) | 0.00% | [9 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5849?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5849      +/-   ##
============================================
- Coverage     66.70%   66.68%   -0.02%     
- Complexity     4482     4484       +2     
============================================
  Files           608      609       +1     
  Lines         16913    16922       +9     
  Branches       3264     3265       +1     
============================================
+ Hits          11282    11285       +3     
- Misses         5182     5190       +8     
+ Partials        449      447       -2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5849?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

TheDarkW3b: Let me know if any changed are required.. also please add the hacktoberfest label 
TheDarkW3b: Let me know if any changes are required.. also please add the hacktoberfest label

Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 833062d1..ac2e921e 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -686,6 +686,7 @@
             * [Isomorphic](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Isomorphic.java)
             * [KMP](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/KMP.java)
             * [LetterCombinationsOfPhoneNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java)
+            * [LongestCommonPrefix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java)
             * [LongestNonRepetitiveSubstring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LongestNonRepetitiveSubstring.java)
             * [LongestPalindromicSubstring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LongestPalindromicSubstring.java)
             * [Lower](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Lower.java)
@@ -1311,6 +1312,7 @@
             * [HorspoolSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/HorspoolSearchTest.java)
             * [IsomorphicTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/IsomorphicTest.java)
             * [LetterCombinationsOfPhoneNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java)
+            * [LongestCommonPrefixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LongestCommonPrefixTest.java)
             * [LongestNonRepetitiveSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LongestNonRepetitiveSubstringTest.java)
             * [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LongestPalindromicSubstringTest.java)
             * [LowerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LowerTest.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 00000000..0fabdaa2
--- /dev/null
+++ b/src/main/java/com/thealgorithms/strings/LongestCommonPrefix.java
@@ -0,0 +1,22 @@
+package com.thealgorithms.strings;
+
+import java.util.Arrays;
+
+public final class LongestCommonPrefix {
+    public String longestCommonPrefix(String[] strs) {
+        if (strs == null || strs.length == 0) {
+            return "";
+        }
+
+        Arrays.sort(strs);
+        String shortest = strs[0];
+        String longest = strs[strs.length - 1];
+
+        int index = 0;
+        while (index < shortest.length() && index < longest.length() && shortest.charAt(index) == longest.charAt(index)) {
+            index++;
+        }
+
+        return shortest.substring(0, index);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/LongestCommonPrefixTest.java b/src/test/java/com/thealgorithms/strings/LongestCommonPrefixTest.java
new file mode 100644
index 00000000..580a2726
--- /dev/null
+++ b/src/test/java/com/thealgorithms/strings/LongestCommonPrefixTest.java
@@ -0,0 +1,73 @@
+package com.thealgorithms.strings;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class LongestCommonPrefixTest {
+
+    private final LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
+
+    @Test
+    public void testCommonPrefix() {
+        String[] input = {"flower", "flow", "flight"};
+        String expected = "fl";
+        assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
+    }
+
+    @Test
+    public void testNoCommonPrefix() {
+        String[] input = {"dog", "racecar", "car"};
+        String expected = "";
+        assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
+    }
+
+    @Test
+    public void testEmptyArray() {
+        String[] input = {};
+        String expected = "";
+        assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
+    }
+
+    @Test
+    public void testNullArray() {
+        String[] input = null;
+        String expected = "";
+        assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
+    }
+
+    @Test
+    public void testSingleString() {
+        String[] input = {"single"};
+        String expected = "single";
+        assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
+    }
+
+    @Test
+    public void testCommonPrefixWithDifferentLengths() {
+        String[] input = {"ab", "a"};
+        String expected = "a";
+        assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
+    }
+
+    @Test
+    public void testAllSameStrings() {
+        String[] input = {"test", "test", "test"};
+        String expected = "test";
+        assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
+    }
+
+    @Test
+    public void testPrefixAtEnd() {
+        String[] input = {"abcde", "abcfgh", "abcmnop"};
+        String expected = "abc";
+        assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
+    }
+
+    @Test
+    public void testMixedCase() {
+        String[] input = {"Flower", "flow", "flight"};
+        String expected = "";
+        assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
+    }
+}