DEV Community

Ertugrul
Ertugrul

Posted on • Edited on

πŸ—“ Daily LeetCode Progress – Day 5

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
Enter fullscreen mode Exit fullscreen mode

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;
    }
};
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
    }
};
Enter fullscreen mode Exit fullscreen mode

Why it works:
The two‑pointer pattern naturally avoids O(n^3) brute force and runs in quadratic time.


πŸ“Έ Achievements

  • Valid Palindrome (Python & C++):

Vp python

Vp cpp

  • 3Sum (Python & C++):

3sum python

3sum cpp


πŸ“¦ 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

Top comments (0)