๐ Two Pointer Problems
Welcome to the Two Pointer section of JDCodebase!
The two-pointer technique is one of the most powerful tools to reduce time complexity in array and string problems. It helps avoid nested loops and makes your solution more efficient.
๐ ๏ธ Basic Approach
- Start with two pointers, usually at the beginning and end of the array (or both at the beginning).
- Move the pointers based on conditions, narrowing the problem space.
- Continue until the pointers meet or cross.
๐ Common Patterns
- Opposite Direction: Start one pointer from the beginning and the other from the end. Useful for problems like reversing, finding pairs, etc.
- Same Direction: Both pointers start from the beginning and one lags behind the other. This is common in sliding window problems.
- Nested Pointer Logic: One pointer iterates and another explores ahead. Used in problems like merging arrays, removing duplicates, etc.
๐ Problems List
- Squares of a Sorted Array (#977)
- Two Sum II - Input array is sorted (#167)
- Merge Sorted Array (#88)
- Move Zeroes (#283)
- Remove Duplicates from Sorted Array (#26)
- Remove Element (#27)
- Sort Colors (#75)
- Reverse String (#344)
- Valid Palindrome (#125)
- String Compression (#443)
- Is Subsequence (#392)
- Merge Strings Alternately (#1768)
- Find the Index of the First Occurrence in a String (#28)
- Reverse Vowels of a String (#345)
- Reverse String II (#541)
- Reverse Words in a String III (#557)
- Count Binary Substrings (#696)
- Shortest Distance to a Character (#821)
- Sort Array by Parity (#905)
- Reverse Only Letters (#917)
- Longest Harmonious Subsequence (#594)