Algorithmscomparison

Two Pointer Comparison — Drive Moves from Values

TT
Testlaa Team
May 14, 20261 min read

You compare nums[left] vs nums[right] (or vs a target) and choose which pointer to move so you never skip the answer.

Try this in Python

def is_subsequence(s: str, t: str) -> bool:
    i = 0
    for ch in t:
        if i < len(s) and s[i] == ch:
            i += 1
    return i == len(s)


print(is_subsequence("ace", "abcde"))

Key takeaways

  • Subsequence uses one pointer on pattern, one implicit scan.
  • Comparison tells when to advance pattern index.
  • Think matching state machine.

Tags:

Two pointersPythonStudents