permalink: /archive/posts/daily_leetcode_1/ date: Tue Sep 20 2022 00:00:00 GMT+0000 (Coordinated Universal Time)
Here's day one of my daily leetcode "challenge", starting with an easy one. Hopefully I keep with this incredible 1 of 1 pace. I've been thinking about doing something like this for Project Euler questions too but I'm so pathetically bad at math I think I would just embarass myself.
Given two strings
t, return true if
sis a subsequence of
t, or false otherwise.
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e.,
"ace"is a subsequence of
This one was listed as easy, and it was indeed pretty simple. It might not be the most elegant solution. but any chance I get to use Python's string slicing I will use it. I basically just make a copy of the substring we're looking for, and if we find the first character of the subsequence in the search string, just splice it out from the subsequence and continue iterating through the looking for the next one. At the end, check if our result is the same as our subsequence, and if so, we found our subsequence inside the search string.
This would work a little different if the subsequence could be in any order, but even then it would still be pretty simple, just iteratively using
subsequence[char] in searchstring would be enough for that.
I don't know if it would help me here, but I really need to look into utilizing Python's
itertools more, since using functional iterators is just so much more elegant (and fun to use).
def isSubsequence(self, s: str, t: str) -> bool:
ps = s
res = ""
for char in t:
if len(s) < 1:
if char == s:
res += s
s = s[1::]
if res == ps: