1
1
package com .thealgorithms .dynamicprogramming ;
2
2
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 () {
6
10
}
7
11
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
+ */
10
21
static int shortestSuperSequence (String x , String y ) {
11
22
int m = x .length ();
12
23
int n = y .length ();
@@ -16,11 +27,20 @@ static int shortestSuperSequence(String x, String y) {
16
27
17
28
// Result is sum of input string
18
29
// lengths - length of lcs
19
- return ( m + n - l ) ;
30
+ return m + n - l ;
20
31
}
21
32
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
+ */
24
44
static int lcs (String x , String y , int m , int n ) {
25
45
int [][] lN = new int [m + 1 ][n + 1 ];
26
46
int i ;
@@ -46,13 +66,4 @@ static int lcs(String x, String y, int m, int n) {
46
66
// for x[0..n - 1] and y[0..m - 1]
47
67
return lN [m ][n ];
48
68
}
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
- }
58
69
}
0 commit comments