Skip to content

Latest commit

 

History

History
61 lines (46 loc) · 2.09 KB

File metadata and controls

61 lines (46 loc) · 2.09 KB

METHOD 1( TLE)

int dfs(int i1, int i2, string t1, string t2,vector<vector<int>>& dp){
        
        if(i1 == t1.length() || i2 == t2.length())
            return 0;
        
        if(dp[i1][i2] != -1)
            return dp[i1][i2]; 
        
        if(t1[i1] == t2[i2])                                    //SAME CHARACTER
            return dp[i1][i2] = 1 + dfs(i1+1, i2+1, t1, t2,dp);
        
        return dp[i1][i2] = max(dfs(i1, i2+1, t1, t2,dp), dfs(i1+1, i2, t1, t2,dp));  
    }
public:
    int longestCommonSubsequence(string t1, string t2) {
        
        int n=t1.size(),m=t2.size();
        vector<vector<int>> dp(n,vector<int>(m,-1));
        return dfs(0, 0, t1, t2,dp);
    }

METHOD 2

 int longestCommonSubsequence(string text1, string text2) {
        int text1Length = text1.size(), text2Length = text2.size();
        // Create a 2D array to store lengths of common subsequence at each index.
        int dp[text1Length + 1][text2Length + 1];
      
        // Initialize the 2D array with zero.
        memset(dp, 0, sizeof dp);
      
        // Loop through both strings and fill the dp array.
        for (int i = 1; i <= text1Length; ++i) {
            for (int j = 1; j <= text2Length; ++j) {
                // If current characters match, add 1 to the length of the sequence
                // until the previous character from both strings.
                if (text1[i - 1] == text2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    // If current characters do not match, take the maximum length
                    // achieved by either skipping the current character of text1 or text2.
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
      
        // Return the value in the bottom-right cell which contains the
        // length of the longest common subsequence for the entire strings.
        return dp[text1Length][text2Length];
    
    }