class Solution:

  def longestCommonSubsequence(self, text1: str, text2: str) -> int:
    if not text1 or not text2:
      return 0
    rows = len(text1) + 1
    cols = len(text2) + 1
    dp = [[0] * cols for _ in range(rows)]
    for row in range(1, rows):
      for col in range(1, cols):
        if text1[row - 1] == text2[col - 1]:
          dp[row][col] = dp[row - 1][col - 1] + 1
        else:
          dp[row][col] = max(dp[row - 1][col], dp[row][col - 1])
    return dp[-1][-1]
