Whenever you have to keep track of two things at once, this is probably the go-to. Of course there are n-pointers problems, but in principle, most of them are the same.
An evolution of the two pointers problem would be binary search
The pattern goes as such,
# based on the defined search space# in this case, the entire listleft = 0right = len(some_list) - 1while left < right: if condition_left: left += 1 elif condition_right: right -= 1 else: # set of condition left += 1 right -= 1
The condition to move the pointers is the tricky part
string = list(s.lower())left = 0right = len(s) - 1while left < right: if not string[left].isalnum(): left += 1 elif not string[right].isalnum(): right -= 1 else: if string[left] != string[right]: return False left += 1 right -= 1return True
We update both left and right simultaneously because we’re checking letters from both ends simultaneously
This is a relatively short but important topic, so make sure this is understood well.