LeetCode and Two Pointers
Hi, Nice to see you! I hope you’re doing well 😄
Today we will learn the Two Pointers technique, a simple yet powerful strategy for traversing arrays or strings.This is must have for solving problems in LeetCode.
Two Pointers
- Use two indices (pointers) to traverse a data structure.
- The pointers don’t have to move in sync — it depends on the problem.
- Common types:
- Opposite ends (start and end of array/string)
- Same direction (slow/fast pointer)
- Expanding around the center
- Common goals:
- meet in the middle (palindrome check; opposite ends)
- move at different speeds (fast/slow pointer, same direction)
- merge two sorted array
In short: choose two indices, check conditions while iterating, and move pointers as needed.
Features:
- In most cases, it requires a sorted array
- Time Complexity: O(n) or O(log n) (depending on the problem)
- Space Complexity: O(1). pretty good
- Pretty basic but fundamental, that also can be used for another techniques (sliding window)
Where to use?
- Text analysis: palindromes, substrings, symmetry.
- Removing duplicates from arrays.
- Searching or filtering data efficiently.
As you can see, this is a must-have technique. It’s not only useful for solving LeetCode problems but also very practical in real-world jobs.
Okay, that’s enough. Let’s practice
Problem: 125. Valid Palindrome
In short: given a string, determine whether it is a palindrome.
- Ignore punctuation, spaces, and symbols.
- Case doesn’t matter (
"S"
and"s"
are the same).
Take 2-3 minutes to think about how to use the two pointers technique to check if a word is a palindrome:
A palindrome is a word/number/sentence that reads the same forwards and backwards.
Think of it as an array that is the same forwards and backwards.
Any ideas?
Approach
For problems that check palindromes, using two pointers from opposite ends solves about 80% of them.
Steps:
- Set two pointers at the ends of the string
- Iterate from the ends to the center using a loop: while leftPtr < rightPtr
- Skip everything except letters and digits
- Compare the characters ignoring case
- Move pointers: leftPtr++(forward) and rightPtr— (backward)
- If no mismutch is found: return true → the string is a palindrome, Congrats! You got it!
Code
class Solution {
public boolean isPalindrome(String s) {
// two pointers: opposite ends
int left = 0, right = s.length() - 1;
while(left < right){
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
left++;
}
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
right--;
}
if(Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))){
return false;
}
left++;
right--;
}
return true;
}
- Time: O(n)
- Space: O(1)
Example:
given: String s = “We panic in a pew!”
Steps:
- Input:
"We panic in a pew!"
-
left = 0
→'W'
(letter) -
right = 17
→'!'
(skip non-letter → move to'w'
) - Compare
'W'
and'w'
→ match → move pointers inward - Continue comparing
'e'
and'e'
, etc. - All matches → return
true
→ the string is a palindrome
Summary – Key Points to Remember
- The two pointers technique is a useful method to traverse arrays or strings, especially for tasks like checking palindromes, removing duplicates, or searching efficiently.
- In most cases, the array should be sorted for optimal use (though this is not always required).
That’s all for this article.
In the next section, I will introduce another technique: sliding window (which also uses the two pointers concept).
Explaining all types of problems for each case would take a lot of text (even for this topic—we haven’t explored checking duplicates yet). I may write another article where I select some problems and briefly explain their approaches.
Also, I want to try to include diagrams, because I know how hard it is to read long text without any visuals 😄
Thanks for reading, and see you next time!
Top comments (0)