Given a string s and a string t, check if s is subsequence of t.



You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (


A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).


Example 1:
s = "abc", t = "ahbgdc"


Return true.


Example 2:
s = "axc", t = "ahbgdc"


Return false.


Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

Credits:Special thanks to @pbrother for adding this problem and creating all test cases.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Dynamic programming

Amortized analysis

Starter code: class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        

Solutions:
class Solution:

	def isSubsequence(self, s, t):
		if len(s) > len(t):
			return False
		for i in s:
			if i in t:
				index = t.find(i)
				t = t[index + 1:]
			else:
				return False
		return True


class Solution:

	def isSubsequence(self, s, t):
		i = 0
		if not s:
			return True
		(m, n) = (len(t), len(s))
		for ch in t:
			if ch == s[i]:
				if i == n - 1:
					return True
				i += 1
		return False


class Solution:

	def isSubsequence(self, s, t):
		length = len(s)
		idx = 0
		for i in range(length):
			idx = t.find(s[i], idx)
			if idx == -1:
				return False
			idx += 1
		return True


class Solution:

	def isSubsequence(self, s, t):
		index = 0
		ans = True
		for i in s:
			if i not in t[index:]:
				ans = False
				break
			else:
				index += t[index:].index(i) + 1
		return ans


class Solution:

	def isSubsequence(self, s, t):
		start = 0
		for i in range(len(s)):
			index = t[start:].find(s[i])
			print(index)
			if index == -1:
				return False
			else:
				index += start
				start = index + 1
		return True


class Solution:

	def isSubsequence(self, s, t):
		t = iter(t)
		return all((c in t for c in s))

	def isSubsequenceFei(self, s, t):
		(p, l) = (0, len(s))
		if l == p:
			return True
		for c_t in t:
			if s[p] == c_t:
				p += 1
			if p == l:
				return True
		return False


class Solution:

	def isSubsequence(self, s, t):
		t = iter(t)
		return all((c in t for c in s))


class Solution:

	def isSubsequence(self, s, t):
		if s == '':
			return True
		s_num = 0
		temp = s[s_num]
		for i in t:
			if i == temp:
				s_num = s_num + 1
				if s_num >= len(s):
					return True
				temp = s[s_num]
		return False


class Solution:

	def isSubsequence(self, s, t):
		iterator = iter(t)
		return all((character in iterator for character in s))


class Solution:

	def isSubsequence(self, s, t):
		if len(s) == 0:
			return True
		if len(s) > len(t):
			return False
		if s[0] not in t:
			return False
		else:
			return self.isSubsequence(s[1:], t[t.index(s[0]) + 1:])