Not a member of GistPad yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- // idea: Find the length of LCS and remove the length from original string
- int n = s.length();
- int m = s2.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(s.charAt(i-1) == s2.charAt(j-1))
- t[i][j] = 1 + t[i-1][j-1];
- else
- }
- }
- int lcsLength = t[n][m];
- return (n-lcsLength);
- }
- }
- // aebcbda
- // adbcbea
- // abcba -> lcs
- // aba
- // aba
RAW Paste Data
Copied
