Skip to content

Commit 7a6ffbb

Browse files
committed
Add is subsequence explanation
1 parent 87bd67f commit 7a6ffbb

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

two-pointers/392_is_subsequence/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,17 @@ def is_subsequence(self, s: str, t: str) -> bool:
5454

5555
* **Time Complexity:** $O(n)$
5656
* **Space Complexity:** $O(1)$
57+
58+
## Explanation of the Solution
59+
60+
1. Initialization:
61+
* `s_pointer` starts at 0 (points to the current character in s being checked).
62+
* `s_length` stores the length of s.
63+
2. Edge Case Handling:
64+
* If `s` is empty, it’s trivially a subsequence of any string (including empty `t`), so return `True`.
65+
3. Iterate Through `t`:
66+
* For each character in `t` (using `t_pointer`):
67+
* If the current character in `t` matches the current character in `s` (at `s_pointer`), move `s_pointer` forward.
68+
* If `s_pointer` reaches the end of `s` (`s_pointer == s_length`), all characters of s were found in order → return `True`.
69+
4. Final Check:
70+
* If the loop ends without `s_pointer` reaching `s_length`, `s` is not a subsequence → return `False`.

0 commit comments

Comments
 (0)