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

  1. Function Definition: The main function is longestCommonSubsequence(s1, s2 string) int,
    which takes two strings as input and returns the length of their LCS.
  2. Dynamic Programming Table: A 2D slice dp is created to store the lengths of
    LCS for different substring combinations.
  3. 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.
  4. 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, where dp[i][j] holds the length of the LCS of the first i characters of s1 and the first j characters of s2.
  • Dynamic Programming Table (dp):
    • The dimensions of the dp table are (m+1) x (n+1), where m and n are the lengths of s1 and s2, respectively.
    • Each cell is filled based on whether the characters of the two strings match.
  • Logic for Filling the DP Table:
    • If the characters at s1[i-1] and s2[j-1] match, the value at dp[i][j] is updated to dp[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 from s1) or dp[i][j-1] (excluding the current character from s2).
  • Function max(a, b int) int:
    • A simple utility function to return the maximum of two integers.

How to Run the Program:

  1. Save the code as main.go.
  2. Open your terminal and navigate to the directory containing the file.
  3. 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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)