Not a member of GistPad yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- /**
- * Find the LCS of both strings
- * Once LCS length is found, we can remove the length of LCS from both the strings
- * and then we can sum up the remaining length of both strings
- */
- int n = word1.length();
- int m = word2.length();
- int[][] t = new int[n+1][m+1];
- for(int i=0; i<n+1; i++)
- {
- for(int j=0; j<m+1; j++)
- {
- if(i == 0 || j == 0)
- t[i][j] = 0;
- }
- }
- for(int i=1; i<n+1; i++)
- {
- for(int j=1; j<m+1; j++)
- {
- if(word1.charAt(i-1) == word2.charAt(j-1))
- {
- t[i][j] = 1 + t[i-1][j-1];
- }
- else
- }
- }
- int lcsLength = t[n][m];
- return (n-lcsLength) + (m-lcsLength);
- }
- }
- // lcs: etco = 4
- // n = 8
- // m = 4
RAW Paste Data
Copied
