Skip to main content
Leetcode Journey

Day 4: Is Subsequence – My Leetcode Journey

Brad Malgas

Brad Malgas

Author

13 August 20252 min read

Learned the importance of handling empty strings. Achieved one of my most optimized solutions at 0ms runtime.

Day 4: Is Subsequence – My Leetcode Journey cover image

Ahh, the age-old issue called life. As expected, life happened and threw me off my game. Still, I intend to push through my “first week,” despite the hiatus.

Today’s problem was 392. Is Subsequence. As usual, I started by writing notes and key takeaways on paper. On paper, the solution looked obvious, but translating that logic to code wasn’t as straightforward.

I noted the following:

  • We only need to check each character in the s string (the subsequence).
  • We look for the current character in s while looping over t.
  • If we reach the end of s, that means every character was found → return true.
  • If we reach the end of t without finishing s, return false (unless both end at the same time).

Based on that, my algorithm was:

  • Set current = 0.
  • Loop over each character in t (call it letter).
  • If s[current] == letter, increment current.
  • If current reaches the end of s, return true.
  • If the loop ends without returning true, return false.

I felt confident but failed the test cases. The flaw: I didn’t handle the case where s is an empty string. Both t and s could be empty. In such cases, the correct return is true (since an empty string is a subsequence of any string). So I added a simple check at the beginning: if s is empty, return true.

The solution took me 20 minutes 8 seconds to complete and had a runtime of 0ms. At first, I thought I had beaten 100% of submissions, but it turns out about 65% of them also had 0ms. So in reality, I only beat 35%. Still, this was probably one of my cleanest and most optimized solutions.

My solution can be found on my GitHub: 392. Is Subsequence.py

Loading reactions…

Loading comments…