Skip to content

Commit b70f077

Browse files
authored
refactor: ShortestCommonSuperSequenceLength (TheAlgorithms#5394)
1 parent 35f23d2 commit b70f077

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
// Java program to find length of the shortest supersequence
4-
final class ShortestSuperSequence {
5-
private ShortestSuperSequence() {
3+
/**
4+
* Class that provides methods to calculate the length of the shortest
5+
* supersequence of two given strings. The shortest supersequence is the smallest string
6+
* that contains both given strings as subsequences.
7+
*/
8+
final class ShortestCommonSuperSequenceLength {
9+
private ShortestCommonSuperSequenceLength() {
610
}
711

8-
// Function to find length of the
9-
// shortest supersequence of x and y.
12+
/**
13+
* Finds the length of the shortest supersequence of two given strings.
14+
* The shortest supersequence is defined as the smallest string that contains both
15+
* given strings as subsequences.
16+
*
17+
* @param x The first input string.
18+
* @param y The second input string.
19+
* @return The length of the shortest supersequence of the two strings.
20+
*/
1021
static int shortestSuperSequence(String x, String y) {
1122
int m = x.length();
1223
int n = y.length();
@@ -16,11 +27,20 @@ static int shortestSuperSequence(String x, String y) {
1627

1728
// Result is sum of input string
1829
// lengths - length of lcs
19-
return (m + n - l);
30+
return m + n - l;
2031
}
2132

22-
// Returns length of LCS
23-
// for x[0..m - 1], y[0..n - 1]
33+
/**
34+
* Calculates the length of the longest common subsequence (LCS) between two strings.
35+
* The LCS is the longest sequence that can be derived from both strings by deleting some
36+
* (or none) of the characters without changing the order of the remaining characters.
37+
*
38+
* @param x The first input string.
39+
* @param y The second input string.
40+
* @param m The length of the first input string.
41+
* @param n The length of the second input string.
42+
* @return The length of the longest common subsequence of the two strings.
43+
*/
2444
static int lcs(String x, String y, int m, int n) {
2545
int[][] lN = new int[m + 1][n + 1];
2646
int i;
@@ -46,13 +66,4 @@ static int lcs(String x, String y, int m, int n) {
4666
// for x[0..n - 1] and y[0..m - 1]
4767
return lN[m][n];
4868
}
49-
50-
// Driver code
51-
public static void main(String[] args) {
52-
String x = "AGGTAB";
53-
String y = "GXTXAYB";
54-
55-
System.out.println("Length of the shortest "
56-
+ "supersequence is " + shortestSuperSequence(x, y));
57-
}
5869
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
public class ShortestCommonSuperSequenceLengthTest {
9+
@ParameterizedTest
10+
@CsvSource({"AGGTAB, GXTXAYB, 9", "ABC, ABC, 3", "ABC, DEF, 6", "'', ABC, 3", "ABCD, AB, 4", "ABC, BCD, 4", "A, B, 2"})
11+
void testShortestSuperSequence(String input1, String input2, int expected) {
12+
assertEquals(expected, ShortestCommonSuperSequenceLength.shortestSuperSequence(input1, input2));
13+
}
14+
}

0 commit comments

Comments
 (0)