int longestCommonSubsequence(string a, string b) {
function<int(int, int)> lcs = [&](int i, int j) -> int {
if (i == (int)a.size() || j == (int)b.size()) return 0;
if (a[i] == b[j]) return 1 + lcs(i + 1, j + 1);
return max(lcs(i + 1, j), lcs(i, j + 1));
};
return lcs(0, 0);
}
public int longestCommonSubsequence(String a, String b) {
return lcs(a, b, 0, 0);
}
private int lcs(String a, String b, int i, int j) {
if (i == a.length() || j == b.length()) return 0;
if (a.charAt(i) == b.charAt(j)) return 1 + lcs(a, b, i + 1, j + 1);
return Math.max(lcs(a, b, i + 1, j), lcs(a, b, i, j + 1));
}
def longest_common_subsequence(a, b):
def lcs(i, j):
if i == len(a) or j == len(b): return 0
if a[i] == b[j]: return 1 + lcs(i + 1, j + 1)
return max(lcs(i + 1, j), lcs(i, j + 1))
return lcs(0, 0)
function longestCommonSubsequence(a: string, b: string): number {
const lcs = (i: number, j: number): number => {
if (i === a.length || j === b.length) return 0;
if (a[i] === b[j]) return 1 + lcs(i + 1, j + 1);
return Math.max(lcs(i + 1, j), lcs(i, j + 1));
};
return lcs(0, 0);
}
int longestCommonSubsequence(string a, string b) {
vector<vector<int>> memo(a.size(), vector<int>(b.size(), -1));
function<int(int, int)> lcs = [&](int i, int j) -> int {
if (i == (int)a.size() || j == (int)b.size()) return 0;
if (memo[i][j] != -1) return memo[i][j];
int result;
if (a[i] == b[j]) result = 1 + lcs(i + 1, j + 1);
else result = max(lcs(i + 1, j), lcs(i, j + 1));
memo[i][j] = result;
return result;
};
return lcs(0, 0);
}
public int longestCommonSubsequence(String a, String b) {
int[][] memo = new int[a.length()][b.length()];
for (int[] line : memo) Arrays.fill(line, -1);
return lcs(a, b, memo, 0, 0);
}
private int lcs(String a, String b, int[][] memo, int i, int j) {
if (i == a.length() || j == b.length()) return 0;
if (memo[i][j] != -1) return memo[i][j];
int result;
if (a.charAt(i) == b.charAt(j)) result = 1 + lcs(a, b, memo, i + 1, j + 1);
else result = Math.max(lcs(a, b, memo, i + 1, j), lcs(a, b, memo, i, j + 1));
memo[i][j] = result;
return result;
}
def longest_common_subsequence(a, b):
memo = [[-1] * len(b) for _ in range(len(a))]
def lcs(i, j):
if i == len(a) or j == len(b): return 0
if memo[i][j] != -1: return memo[i][j]
if a[i] == b[j]: result = 1 + lcs(i + 1, j + 1)
else: result = max(lcs(i + 1, j), lcs(i, j + 1))
memo[i][j] = result
return result
return lcs(0, 0)
function longestCommonSubsequence(a: string, b: string): number {
const memo = Array.from({ length: a.length }, () => new Array<number>(b.length).fill(-1));
const lcs = (i: number, j: number): number => {
if (i === a.length || j === b.length) return 0;
if (memo[i][j] !== -1) return memo[i][j];
let result: number;
if (a[i] === b[j]) result = 1 + lcs(i + 1, j + 1);
else result = Math.max(lcs(i + 1, j), lcs(i, j + 1));
memo[i][j] = result;
return result;
};
return lcs(0, 0);
}
int longestCommonSubsequence(string a, string b) {
int m = a.size(), n = b.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (a[i] == b[j]) dp[i][j] = 1 + dp[i + 1][j + 1];
else dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
}
}
return dp[0][0];
}
public int longestCommonSubsequence(String a, String b) {
int m = a.length(), n = b.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (a.charAt(i) == b.charAt(j)) dp[i][j] = 1 + dp[i + 1][j + 1];
else dp[i][j] = Math.max(dp[i + 1][j], dp[i][j + 1]);
}
}
return dp[0][0];
}
def longest_common_subsequence(a, b):
m, n = len(a), len(b)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
if a[i] == b[j]: dp[i][j] = 1 + dp[i + 1][j + 1]
else: dp[i][j] = max(dp[i + 1][j], dp[i][j + 1])
return dp[0][0]
function longestCommonSubsequence(a: string, b: string): number {
const m = a.length, n = b.length;
const dp = Array.from({ length: m + 1 }, () => new Array<number>(n + 1).fill(0));
for (let i = m - 1; i >= 0; i--) {
for (let j = n - 1; j >= 0; j--) {
if (a[i] === b[j]) dp[i][j] = 1 + dp[i + 1][j + 1];
else dp[i][j] = Math.max(dp[i + 1][j], dp[i][j + 1]);
}
}
return dp[0][0];
}
int longestCommonSubsequence(string a, string b) {
if (b.size() > a.size()) swap(a, b);
vector<int> next(b.size() + 1, 0);
vector<int> cur(b.size() + 1, 0);
for (int i = (int)a.size() - 1; i >= 0; i--) {
for (int j = (int)b.size() - 1; j >= 0; j--) {
cur[j] = a[i] == b[j] ? 1 + next[j + 1] : max(next[j], cur[j + 1]);
}
swap(cur, next);
}
return next[0];
}
public int longestCommonSubsequence(String a, String b) {
if (b.length() > a.length()) { String t = a; a = b; b = t; }
int[] next = new int[b.length() + 1];
int[] cur = new int[b.length() + 1];
for (int i = a.length() - 1; i >= 0; i--) {
for (int j = b.length() - 1; j >= 0; j--) {
cur[j] = a.charAt(i) == b.charAt(j) ? 1 + next[j + 1] : Math.max(next[j], cur[j + 1]);
}
int[] tmp = cur; cur = next; next = tmp;
}
return next[0];
}
def longest_common_subsequence(a, b):
if len(b) > len(a): a, b = b, a
nxt = [0] * (len(b) + 1)
cur = [0] * (len(b) + 1)
for i in range(len(a) - 1, -1, -1):
for j in range(len(b) - 1, -1, -1):
cur[j] = 1 + nxt[j + 1] if a[i] == b[j] else max(nxt[j], cur[j + 1])
cur, nxt = nxt, cur
return nxt[0]
function longestCommonSubsequence(a: string, b: string): number {
if (b.length > a.length) [a, b] = [b, a];
let next = new Array<number>(b.length + 1).fill(0);
let cur = new Array<number>(b.length + 1).fill(0);
for (let i = a.length - 1; i >= 0; i--) {
for (let j = b.length - 1; j >= 0; j--) {
cur[j] = a[i] === b[j] ? 1 + next[j + 1] : Math.max(next[j], cur[j + 1]);
}
[cur, next] = [next, cur];
}
return next[0];
}