Not a member of GistPad yet?
Sign Up,
it unlocks many cool features!
- // User function Template for Java
- 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 = s1.length();
- int m = s2.length();
- // step-1 create a matrix of LCS
- int[][] t = new int[n+1][m+1];
- // step-2 initialise the matrix
- for(int i=0; i<n+1; i++)
- {
- for(int j=0; j<m+1; j++)
- {
- // when string is empty, no LCS found
- if(i == 0 || j == 0)
- t[i][j] = 0;
- }
- }
- // step-3 LCS iterative code
- for(int i=1; i<n+1; i++)
- {
- for(int j=1; j<m+1; j++)
- {
- if(s1.charAt(i-1) == s2.charAt(j-1))
- t[i][j] = 1 + t[i-1][j-1];
- else
- }
- }
- int lcsLength = t[n][m];
- int deletion = n-lcsLength;
- int insertion = m-lcsLength;
- return deletion+insertion;
- }
- }
RAW Paste Data
Copied
