
Tips for Two Pointers:
- The classic pattern for a two-pointer sliding window: keep moving the right pointer to the right until it can no longer move further (the exact condition depends on the problem). Once the right pointer reaches the far right, start moving the left pointer to shrink/release the left boundary of the window. Problems 3, 76, 209, 424, 438, 567, 713, 763, 845, 881, 904, 978, 992, 1004, 1040, and 1052.
left, right := 0, -1
for left < len(s) {
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
freq[s[right+1]-'a']++
right++
} else {
freq[s[left]-'a']--
left++
}
result = max(result, right-left+1)
}
- Fast and slow pointers can be used to find duplicate numbers, with time complexity O(n). Problem 287.
- After replacing letters, find the maximum length of a contiguous segment containing the same letter. Problem 424.
- SUM problem set. Problem 1, Problem 15, Problem 16, Problem 18, Problem 167, Problem 923, Problem 1074.
