The concise version:
class Solution(object): def isSubsequence(self, s, t): i, j = 0, 0 while i<len(s) and j<len(t): i += s[i]==t[j] j += 1 return i==len(s)
The fast version (10 times faster, but still pretty concise):
class Solution(object): def isSubsequence(self, s, t): for c in s: i = t.find(c) if i == -1: return False t = t[i+1:] return True
find function has an optional
@StefanPochmann You are right. I just realized. Thanks.
@StefanPochmann I can't be lazy for just one time, hur? Hahaha.
But yeah, you're right. We can keep track of the start index, but that's one more line.
@o_sharp It was just odd to see you do that in a version explicitly intended to be fast :-). And keeping track of the start index doesn't need one more line, can easily be done (and I think has already been done by others) with the same number of lines.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.