Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,41 @@

import java.util.Arrays;

/**
* Utility class for string operations.
* <p>
* This class provides a method to find the longest common prefix (LCP)
* among an array of strings.
* </p>
*
* @see <a href="https://en.wikipedia.org/wiki/Longest_common_prefix">Longest Common Prefix - Wikipedia</a>
*/
public final class LongestCommonPrefix {
public String longestCommonPrefix(String[] strs) {

private LongestCommonPrefix() {
}

/**
* Finds the longest common prefix among a list of strings using lexicographical sorting.
* The prefix is common to the first and last elements after sorting the array.
*
* @param strs array of input strings
* @return the longest common prefix, or empty string if none exists
*/
public static String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}

Arrays.sort(strs);
String shortest = strs[0];
String longest = strs[strs.length - 1];
String first = strs[0];
String last = strs[strs.length - 1];

int index = 0;
while (index < shortest.length() && index < longest.length() && shortest.charAt(index) == longest.charAt(index)) {
while (index < first.length() && index < last.length() && first.charAt(index) == last.charAt(index)) {
index++;
}

return shortest.substring(0, index);
return first.substring(0, index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,23 @@

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

import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

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));
@ParameterizedTest(name = "{index} => input={0}, expected=\"{1}\"")
@MethodSource("provideTestCases")
@DisplayName("Test Longest Common Prefix")
void testLongestCommonPrefix(String[] input, String expected) {
assertEquals(expected, LongestCommonPrefix.longestCommonPrefix(input));
}

@Test
public void testMixedCase() {
String[] input = {"Flower", "flow", "flight"};
String expected = "";
assertEquals(expected, longestCommonPrefix.longestCommonPrefix(input));
private static Stream<Arguments> provideTestCases() {
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"),
Arguments.of(new String[] {"test", "test", "test"}, "test"), Arguments.of(new String[] {"abcde", "abcfgh", "abcmnop"}, "abc"), Arguments.of(new String[] {"Flower", "flow", "flight"}, ""));
}
}
Loading