MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / LCS

Method LCS

java/1143-longest-common-subsequence.java:10–20  ·  view source on GitHub ↗
(String s1, String s2, int i, int j, int[][] dp)

Source from the content-addressed store, hash-verified

8 }
9
10 public int LCS(String s1, String s2, int i, int j, int[][] dp) {
11 if (i >= s1.length() || j >= s2.length()) return 0; else if (
12 dp[i][j] != 0
13 ) return dp[i][j]; else if (s1.charAt(i) == s2.charAt(j)) return (
14 1 + LCS(s1, s2, i + 1, j + 1, dp)
15 ); else {
16 dp[i][j] =
17 Math.max(LCS(s1, s2, i + 1, j, dp), LCS(s1, s2, i, j + 1, dp));
18 return dp[i][j];
19 }
20 }
21}
22
23// Iterative version

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected