refactor: ShortestCommonSuperSequenceLength (#5394)

This commit is contained in:
Alex Klymenko 2024-08-26 10:50:12 +02:00 committed by GitHub
parent 35f23d2ddc
commit b70f077343
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 17 deletions

View File

@ -1,12 +1,23 @@
package com.thealgorithms.dynamicprogramming; package com.thealgorithms.dynamicprogramming;
// Java program to find length of the shortest supersequence /**
final class ShortestSuperSequence { * Class that provides methods to calculate the length of the shortest
private ShortestSuperSequence() { * supersequence of two given strings. The shortest supersequence is the smallest string
* that contains both given strings as subsequences.
*/
final class ShortestCommonSuperSequenceLength {
private ShortestCommonSuperSequenceLength() {
} }
// Function to find length of the /**
// shortest supersequence of x and y. * Finds the length of the shortest supersequence of two given strings.
* The shortest supersequence is defined as the smallest string that contains both
* given strings as subsequences.
*
* @param x The first input string.
* @param y The second input string.
* @return The length of the shortest supersequence of the two strings.
*/
static int shortestSuperSequence(String x, String y) { static int shortestSuperSequence(String x, String y) {
int m = x.length(); int m = x.length();
int n = y.length(); int n = y.length();
@ -16,11 +27,20 @@ final class ShortestSuperSequence {
// Result is sum of input string // Result is sum of input string
// lengths - length of lcs // lengths - length of lcs
return (m + n - l); return m + n - l;
} }
// Returns length of LCS /**
// for x[0..m - 1], y[0..n - 1] * Calculates the length of the longest common subsequence (LCS) between two strings.
* The LCS is the longest sequence that can be derived from both strings by deleting some
* (or none) of the characters without changing the order of the remaining characters.
*
* @param x The first input string.
* @param y The second input string.
* @param m The length of the first input string.
* @param n The length of the second input string.
* @return The length of the longest common subsequence of the two strings.
*/
static int lcs(String x, String y, int m, int n) { static int lcs(String x, String y, int m, int n) {
int[][] lN = new int[m + 1][n + 1]; int[][] lN = new int[m + 1][n + 1];
int i; int i;
@ -46,13 +66,4 @@ final class ShortestSuperSequence {
// for x[0..n - 1] and y[0..m - 1] // for x[0..n - 1] and y[0..m - 1]
return lN[m][n]; return lN[m][n];
} }
// Driver code
public static void main(String[] args) {
String x = "AGGTAB";
String y = "GXTXAYB";
System.out.println("Length of the shortest "
+ "supersequence is " + shortestSuperSequence(x, y));
}
} }

View File

@ -0,0 +1,14 @@
package com.thealgorithms.dynamicprogramming;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class ShortestCommonSuperSequenceLengthTest {
@ParameterizedTest
@CsvSource({"AGGTAB, GXTXAYB, 9", "ABC, ABC, 3", "ABC, DEF, 6", "'', ABC, 3", "ABCD, AB, 4", "ABC, BCD, 4", "A, B, 2"})
void testShortestSuperSequence(String input1, String input2, int expected) {
assertEquals(expected, ShortestCommonSuperSequenceLength.shortestSuperSequence(input1, input2));
}
}