From b70f077343a4629cf163400f1d64993c6ab2bbf7 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Mon, 26 Aug 2024 10:50:12 +0200 Subject: [PATCH] refactor: `ShortestCommonSuperSequenceLength` (#5394) --- .../ShortestCommonSupersequenceLength.java | 45 ++++++++++++------- ...ShortestCommonSuperSequenceLengthTest.java | 14 ++++++ 2 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/ShortestCommonSuperSequenceLengthTest.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java b/src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java index 362ed5e2..3ea440ca 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/ShortestCommonSupersequenceLength.java @@ -1,12 +1,23 @@ package com.thealgorithms.dynamicprogramming; -// Java program to find length of the shortest supersequence -final class ShortestSuperSequence { - private ShortestSuperSequence() { +/** + * Class that provides methods to calculate the length of the shortest + * 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) { int m = x.length(); int n = y.length(); @@ -16,11 +27,20 @@ final class ShortestSuperSequence { // Result is sum of input string // 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) { int[][] lN = new int[m + 1][n + 1]; int i; @@ -46,13 +66,4 @@ final class ShortestSuperSequence { // for x[0..n - 1] and y[0..m - 1] 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)); - } } diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/ShortestCommonSuperSequenceLengthTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/ShortestCommonSuperSequenceLengthTest.java new file mode 100644 index 00000000..007e71e2 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/ShortestCommonSuperSequenceLengthTest.java @@ -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)); + } +}