# Leetcode 1143 - Longest common subsequence

`Note`

`dp[i][j]`

means the max length of common subsequence between`text1 [0, i-1]`

and`text2 [0, j-1]`

(Doesn’t need to end with the cur char)- Create an
`(text1.length+1) x (text1.length+1)`

dp array

- Create an
- Initialize all elements as
`0`

. - To skip initializing first row/col manually, the
is to iterate from`techinique`

`i=1`

and`j=1`

but compare`text1[i-1]`

and`text2[j-1]`

. In this way, we will initialize the first row/col in our for loops. - By checking the table we drew, it’s easier to find the deduction.
- When
`text1[i-1] === text2[j-1]`

,`dp[i][j] = dp[i-1][j-1] + 1`

. - When
`text1[i-1] !== text2[j-1]`

,`dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]).`

- When

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

- For example, “ace” is a subsequence of “abcde”.

A common subsequence of two strings is a subsequence that is common to both strings.

**Example**

1 | Input: text1 = "abcde", text2 = "ace" |

1 | /** |