Problems Solved:
- #125 Valid Palindrome
- #15 3Sum
This continues my daily series (Day 5: String + Two Pointers + Sorting). Today is a special day π β Iβve officially hit my 5βday streak of solving and documenting problems. My goal is to keep this streak going consistently and build strong problemβsolving momentum.
π‘ What I Learned
Todayβs focus was on two classic patterns:
- String cleaning + twoβpointer palindrome check (ignoring nonβalphanumeric characters).
- Sorting + twoβpointer pair search inside a triple loop for 3Sum.
- Practiced careful duplicate skipping logic to avoid redundant answers.
- Saw how the same twoβpointer pattern can apply to very different problem types (string vs. array sum).
π§© #125 β Valid Palindrome
Python (Two Pointers)
class Solution:
def isPalindrome(self, s: str) -> bool:
s = ''.join(c.lower() for c in s if c.isalnum())
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
Why it works:
We filter out nonβalphanumeric characters and lowercase everything, then apply the twoβpointer inward check.
Time: O(n)
Space: O(n)
(for the cleaned string)
C++ (Two Pointers)
class Solution {
public:
bool isPalindrome(string s) {
string result;
result.reserve(s.size());
for (char c : s) {
if (std::isalnum(static_cast<unsigned char>(c))) {
result.push_back(std::tolower(static_cast<unsigned char>(c)));
}
}
int left = 0, right = result.size() - 1;
while (left < right) {
if (result[left] != result[right]) {
return false;
}
left++;
right--;
}
return true;
}
};
Why it works:
Same principle as Python: clean the string, then run twoβpointer comparison.
π§© #15 β 3Sum
Python (Sorting + Two Pointers)
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
n = len(nums)
res = []
for i in range(n - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
if nums[i] > 0:
break
l, r = i + 1, n - 1
while l < r:
total = nums[i] + nums[l] + nums[r]
if total < 0:
l += 1
elif total > 0:
r -= 1
else:
res.append([nums[i], nums[l], nums[r]])
left_val, right_val = nums[l], nums[r]
while l < r and nums[l] == left_val:
l += 1
while l < r and nums[r] == right_val:
r -= 1
return res
Why it works:
Sort first, then fix one element and find complementary pairs with two pointers. Duplicate skipping is key.
Time: O(n^2)
Space: O(1)
(extra, result excluded)
C++ (Sorting + Two Pointers)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
const int n = nums.size();
for (int i = 0; i < n - 2; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
if (nums[i] > 0) break;
int l = i + 1, r = n - 1;
while (l < r) {
long long total = (long long)nums[i] + nums[l] + nums[r];
if (total < 0) {
++l;
} else if (total > 0) {
--r;
} else {
res.push_back({nums[i], nums[l], nums[r]});
int lastL = nums[l], lastR = nums[r];
while (l < r && nums[l] == lastL) ++l;
while (l < r && nums[r] == lastR) --r;
}
}
}
return res;
}
};
Why it works:
The twoβpointer pattern naturally avoids O(n^3)
brute force and runs in quadratic time.
πΈ Achievements
- Valid Palindrome (Python & C++):
- 3Sum (Python & C++):
π¦ Complexity Recap
-
Valid Palindrome:
O(n)
time,O(n)
space -
3Sum:
O(n^2)
time,O(1)
space
π£ Join the Journey
Day 5 streak β and counting! Iβm solving and documenting problems daily in both Python and C++, highlighting patterns and performance insights. Follow along if youβre into algorithms and efficiency.
Links
- LinkedIn: https://www.linkedin.com/in/ertugrul-mutlu
- GitHub Series: https://github.com/Ertugrulmutlu/leetcode-series
Top comments (0)