Is Subsequence

Intuition Another way to read the problem is: find if all s characters are present in t, in the same order. It doesn’t have to be consecutive. We need to keep two pointers: One for the string t One for the string s We got loop through the string t, and if t[tPointer] == s[iPointer], we increase both. If t[tPointer] != s[iPointer], will increase just tPointer. At the end if iPointer==len(s) we reach end of s, we are good, return True This will be solved in time O(n) and space(1), since is one pass and we don’t use additional structures other than the pointers. ...

April 19, 2025 · 2 min

Linked List Cycle

Intuition Do a hash on the value itself, but this has a flaw that different nodes can have the same value, therefore not forming a cycle. A optiomal approach is to use the slow-fast approach. One pointer jumping one node at a time, the other jumping two. If the faster points reaches the end (node.next == null), then we don’t have a cycle. When both slow and fast meets each other, we found our cycle. ...

April 16, 2025 · 2 min