You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* @return: The length of longest common subsequence of A and B
*/
public int longestCommonSubsequence(String A, String B) {
char[] str = A.toCharArray();
char[] ttr = B.toCharArray();
int[] curr = new int[B.length() + 1];
for (int a = 0; a < A.length(); a++) { //compare each char of A
int[] next = new int[B.length() + 1];
for (int b = 0; b < B.length(); b++) { //with each char of B
if (str[a] == ttr[b]) {
next[b + 1] = curr[b] + 1; //found a match
} else {
next[b + 1] = Math.max(curr[b + 1], next[b]); //if A only spanned from 0 to a and B spanned from 0 to b, next[b+1] tracks LCS, either inherited from a shorter B or a shorter A