class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
def rec(i, j):
if i == len(s):
return True
if j == len(t):
return False
if s[i] == t[j]:
return rec(i + 1, j + 1)
return rec(i, j + 1)
return rec(0, 0)Where is the length of the string and is the length of the string .
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
n, m = len(s), len(t)
memo = [[-1] * m for _ in range(n)]
def rec(i, j):
if i == n:
return True
if j == m:
return False
if memo[i][j] != -1:
return memo[i][j] == 1
if s[i] == t[j]:
memo[i][j] = 1 if rec(i + 1, j + 1) else 0
else:
memo[i][j] = 1 if rec(i, j + 1) else 0
return memo[i][j] == 1
return rec(0, 0)Where is the length of the string and is the length of the string .
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
n, m = len(s), len(t)
dp = [[False] * (m + 1) for _ in range(n + 1)]
for j in range(m + 1):
dp[n][j] = True
for i in range(n - 1, -1, -1):
for j in range(m - 1, -1, -1):
if s[i] == t[j]:
dp[i][j] = dp[i + 1][j + 1]
else:
dp[i][j] = dp[i][j + 1]
return dp[0][0]Where is the length of the string and is the length of the string .
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
i = j = 0
while i < len(s) and j < len(t):
if s[i] == t[j]:
i += 1
j += 1
return i == len(s)Where is the length of the string and is the length of the string .
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
n, m = len(s), len(t)
if m == 0:
return n == 0
store = [[m + 1] * 26 for _ in range(m)]
store[m - 1][ord(t[m - 1]) - ord('a')] = m - 1
for i in range(m - 2, -1, -1):
store[i] = store[i + 1][:]
store[i][ord(t[i]) - ord('a')] = i
i, j = 0, 0
while i < n and j < m:
j = store[j][ord(s[i]) - ord('a')] + 1
if j > m:
return False
i += 1
return i == nWhere is the length of the string and is the length of the string .