Skip to content

Commit 048bba9

Browse files
refactor: adding docs for LongestCommonPrefixTest and Parameterized Tests (TheAlgorithms#6360)
* refactor: adding docs for LongestCommonPrefixTest and Parameterized Tests * checkstyle: fix clang formatting --------- Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
1 parent 3e0fd11 commit 048bba9

File tree

2 files changed

+38
-67
lines changed

2 files changed

+38
-67
lines changed

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,41 @@
22

33
import java.util.Arrays;
44

5+
/**
6+
* Utility class for string operations.
7+
* <p>
8+
* This class provides a method to find the longest common prefix (LCP)
9+
* among an array of strings.
10+
* </p>
11+
*
12+
* @see <a href="https://en.wikipedia.org/wiki/Longest_common_prefix">Longest Common Prefix - Wikipedia</a>
13+
*/
514
public final class LongestCommonPrefix {
6-
public String longestCommonPrefix(String[] strs) {
15+
16+
private LongestCommonPrefix() {
17+
}
18+
19+
/**
20+
* Finds the longest common prefix among a list of strings using lexicographical sorting.
21+
* The prefix is common to the first and last elements after sorting the array.
22+
*
23+
* @param strs array of input strings
24+
* @return the longest common prefix, or empty string if none exists
25+
*/
26+
public static String longestCommonPrefix(String[] strs) {
727
if (strs == null || strs.length == 0) {
828
return "";
929
}
1030

1131
Arrays.sort(strs);
12-
String shortest = strs[0];
13-
String longest = strs[strs.length - 1];
32+
String first = strs[0];
33+
String last = strs[strs.length - 1];
1434

1535
int index = 0;
16-
while (index < shortest.length() && index < longest.length() && shortest.charAt(index) == longest.charAt(index)) {
36+
while (index < first.length() && index < last.length() && first.charAt(index) == last.charAt(index)) {
1737
index++;
1838
}
1939

20-
return shortest.substring(0, index);
40+
return first.substring(0, index);
2141
}
2242
}

src/test/java/com/thealgorithms/strings/LongestCommonPrefixTest.java

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,23 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
610

711
public class LongestCommonPrefixTest {
812

9-
private final LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
10-
11-
@Test
12-
public void testCommonPrefix() {
13-
String[] input = {"flower", "flow", "flight"};
14-
String expected = "fl";
15-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
16-
}
17-
18-
@Test
19-
public void testNoCommonPrefix() {
20-
String[] input = {"dog", "racecar", "car"};
21-
String expected = "";
22-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
23-
}
24-
25-
@Test
26-
public void testEmptyArray() {
27-
String[] input = {};
28-
String expected = "";
29-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
30-
}
31-
32-
@Test
33-
public void testNullArray() {
34-
String[] input = null;
35-
String expected = "";
36-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
37-
}
38-
39-
@Test
40-
public void testSingleString() {
41-
String[] input = {"single"};
42-
String expected = "single";
43-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
44-
}
45-
46-
@Test
47-
public void testCommonPrefixWithDifferentLengths() {
48-
String[] input = {"ab", "a"};
49-
String expected = "a";
50-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
51-
}
52-
53-
@Test
54-
public void testAllSameStrings() {
55-
String[] input = {"test", "test", "test"};
56-
String expected = "test";
57-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
58-
}
59-
60-
@Test
61-
public void testPrefixAtEnd() {
62-
String[] input = {"abcde", "abcfgh", "abcmnop"};
63-
String expected = "abc";
64-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
13+
@ParameterizedTest(name = "{index} => input={0}, expected=\"{1}\"")
14+
@MethodSource("provideTestCases")
15+
@DisplayName("Test Longest Common Prefix")
16+
void testLongestCommonPrefix(String[] input, String expected) {
17+
assertEquals(expected, LongestCommonPrefix.longestCommonPrefix(input));
6518
}
6619

67-
@Test
68-
public void testMixedCase() {
69-
String[] input = {"Flower", "flow", "flight"};
70-
String expected = "";
71-
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
20+
private static Stream<Arguments> provideTestCases() {
21+
return Stream.of(Arguments.of(new String[] {"flower", "flow", "flight"}, "fl"), Arguments.of(new String[] {"dog", "racecar", "car"}, ""), Arguments.of(new String[] {}, ""), Arguments.of(null, ""), Arguments.of(new String[] {"single"}, "single"), Arguments.of(new String[] {"ab", "a"}, "a"),
22+
Arguments.of(new String[] {"test", "test", "test"}, "test"), Arguments.of(new String[] {"abcde", "abcfgh", "abcmnop"}, "abc"), Arguments.of(new String[] {"Flower", "flow", "flight"}, ""));
7223
}
7324
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy