Program Explanation
This program finds the longest common subsequence (LCS) between two strings using dynamic programming.
The LCS is defined as the longest sequence that appears in the same relative order in both strings,
but not necessarily consecutively. The dynamic programming approach builds a table that stores the
lengths of LCS for different pairs of prefixes of the two strings, allowing us to compute the result efficiently.
Program Structure
- Function Definition: The main function is
longestCommonSubsequence(s1, s2 string) int
,
which takes two strings as input and returns the length of their LCS. - Dynamic Programming Table: A 2D slice
dp
is created to store the lengths of
LCS for different substring combinations. - Logic: Nested loops iterate through the characters of both strings:
- If characters match, the value is taken from the top-left diagonal cell plus one.
- If they don’t match, the maximum value from the left or top cell is taken.
- Result: The length of the longest common subsequence is found at the bottom-right cell
of the DP table.
Go Code
package main
import (
"fmt"
)
// longestCommonSubsequence finds the length of the longest common subsequence of two strings.
func longestCommonSubsequence(s1, s2 string) int {
m := len(s1)
n := len(s2)
// Create a DP table to store lengths of longest common subsequence
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
}
// Fill the DP table
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ { if s1[i-1] == s2[j-1] { dp[i][j] = dp[i-1][j-1] + 1 } else { dp[i][j] = max(dp[i-1][j], dp[i][j-1]) } } } // The length of the longest common subsequence is in dp[m][n] return dp[m][n] } // max returns the maximum of two integers. func max(a, b int) int { if a > b {
return a
}
return b
}
func main() {
// Example strings
s1 := "AGGTAB"
s2 := "GXTXAYB"
lcsLength := longestCommonSubsequence(s1, s2)
fmt.Printf("Length of Longest Common Subsequence: %d\n", lcsLength)
}
Explanation of the Code:
- Function
longestCommonSubsequence(s1, s2 string) int
:- This function computes the length of the longest common subsequence between two strings.
- It initializes a 2D slice
dp
, wheredp[i][j]
holds the length of the LCS of the firsti
characters ofs1
and the firstj
characters ofs2
.
- Dynamic Programming Table (
dp
):- The dimensions of the
dp
table are(m+1) x (n+1)
, wherem
andn
are the lengths ofs1
ands2
, respectively. - Each cell is filled based on whether the characters of the two strings match.
- The dimensions of the
- Logic for Filling the DP Table:
- If the characters at
s1[i-1]
ands2[j-1]
match, the value atdp[i][j]
is updated todp[i-1][j-1] + 1
. - If they don’t match, it takes the maximum value from either
dp[i-1][j]
(excluding the current character froms1
) ordp[i][j-1]
(excluding the current character froms2
).
- If the characters at
- Function
max(a, b int) int
:- A simple utility function to return the maximum of two integers.
How to Run the Program:
- Save the code as
main.go
. - Open your terminal and navigate to the directory containing the file.
- Run the command
go run main.go
.
This program efficiently solves the longest common subsequence problem and can be a useful reference for anyone working with string manipulation algorithms.
Conclusion
This Go program efficiently finds the longest common subsequence between two strings using a dynamic
programming approach. The algorithm has a time complexity of O(m*n), where m and n are the lengths of
the two strings. This method is optimal for solving the LCS problem, providing both the length of the
subsequence and a foundation for further exploration of related problems.