2 strings s and t, one only has to tell whether a string s is subsequence of t or not
What is a subsequence
a string or number s is subsequence of t if all the characters or number occur in t and they should just maintain relative order, there is no need to maintain continuity.
- in this we will use 2 pointer where we will be putting both pointer at start of both the strings
- using loop iteration we will be comparing them char by char
- when they are equal we will increment both, if not we will increment t.
- in the end we will return if all are found .
Trigger: Checking relative order preservation across two strings. Mechanical Approach: Two Pointers.
- Pointer
ions, pointerjont. - If
s[i] == t[j], move both. Else movej. - Valid if
ifully traversess(i == len(s)).
The System/Scale Follow-up:
- Scenario: 1 massive string
t, incoming short stringss. - Architecture: Don’t rescan
t. Precomputetinto a Hash Map ofchar -> [list of indices]. - Execution: For each char in
s, use Binary Search (bisect_right) on the Hash Map’s index list to find the next valid, strictly greater index in time.
Blindspot: Always handle s = "" early. It should return True.