Skip to content

Commit ba9f7a2

Browse files
committed
fix: resolve build errors and formatting
1 parent 8d74a32 commit ba9f7a2

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.thealgorithms.strings;
22

33
/**
4-
* Longest Common Substring finds the longest string that is a
4+
* Longest Common Substring finds the longest string that is a
55
* contiguous substring of two input strings.
6-
* Example: "abcdef" and "zcdemf" "cde"
6+
* Example: "abcdef" and "zcdemf" -> "cde"
77
*/
88
public final class LongestCommonSubstring {
99

@@ -22,9 +22,11 @@ public static String longestCommonSubstring(final String a, final String b) {
2222
if (a == null || b == null || a.isEmpty() || b.isEmpty()) {
2323
return "";
2424
}
25+
2526
int[][] dp = new int[a.length() + 1][b.length() + 1];
2627
int maxLength = 0;
2728
int endIndex = 0;
29+
2830
for (int i = 1; i <= a.length(); i++) {
2931
for (int j = 1; j <= b.length(); j++) {
3032
if (a.charAt(i - 1) == b.charAt(j - 1)) {
@@ -33,6 +35,8 @@ public static String longestCommonSubstring(final String a, final String b) {
3335
maxLength = dp[i][j];
3436
endIndex = i;
3537
}
38+
} else {
39+
dp[i][j] = 0;
3640
}
3741
}
3842
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package com.thealgorithms.strings;
22

33
import java.util.stream.Stream;
4+
45
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.params.ParameterizedTest;
67
import org.junit.jupiter.params.provider.MethodSource;
78

8-
public class LongestCommonSubstringTest {
9+
public final class LongestCommonSubstringTest {
910

1011
private record TestData(String a, String b, String expected) {
1112
}
1213

13-
private static Stream<TestData> provideTestCases() {
14+
// JUnit 5 matches this automatically because it has the same name as the test method
15+
private static Stream<TestData> testLongestCommonSubstring() {
1416
return Stream.of(
1517
new TestData(null, "abc", ""),
1618
new TestData("abc", null, ""),
@@ -20,14 +22,14 @@ private static Stream<TestData> provideTestCases() {
2022
new TestData("abc", "abc", "abc"),
2123
new TestData("abc", "xyz", ""),
2224
new TestData("abcdef", "cdefgh", "cdef"),
23-
new TestData("a", "a", "a")
25+
new TestData("a", "a", "a"),
26+
new TestData("abcXdef", "abcYdef", "abc")
2427
);
2528
}
2629

2730
@ParameterizedTest
28-
@MethodSource("provideTestCases")
31+
@MethodSource // No string argument needed here anymore
2932
void testLongestCommonSubstring(TestData testData) {
30-
Assertions.assertEquals(testData.expected(),
31-
LongestCommonSubstring.longestCommonSubstring(testData.a(), testData.b()));
33+
Assertions.assertEquals(testData.expected(), LongestCommonSubstring.longestCommonSubstring(testData.a(), testData.b()));
3234
}
3335
}

0 commit comments

Comments
 (0)