Find the length of longest common subsequence and subtract from both String's lengths and then add both values.

int l1 = word1.length();
        int l2 = word2.length();
        char c1[] = word1.toCharArray();
        char c2[] = word2.toCharArray();
        
        int dp[][] = new int[l1+1][l2+1];
        for(int i=1;i<=l1;i++){
            for(int j=1;j<=l2;j++){
                if(c1[i-1] == c2[j-1]){
                    dp[i][j] = 1 + dp[i-1][j-1];
                }
                else{
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        
        int lcs = dp[l1][l2];
        
        
        return word1.length()-lcs + word2.length()-lcs;
		```
Comments (0)