DEV Community

Ertugrul
Ertugrul

Posted on

πŸ—“ Daily LeetCode Progress – Day 13

Problems Solved:

  • #409 Longest Palindrome
  • #141 Linked List Cycle

This continues my daily series (Day 14: Hashing + Two-Pointer Cycle Detection). Today I worked on string hashing to maximize palindrome length and on pointer-based cycle detection in linked lists.


πŸ’‘ What I Learned

  • For Longest Palindrome, counting character frequencies is enough. Every pair of characters contributes to the palindrome, and if any odd count exists, we can place exactly one odd character in the center.
  • For Linked List Cycle, the classic Floyd’s Tortoise and Hare Algorithm solves it efficiently. Two pointers moving at different speeds will eventually meet if there is a cycle.
  • Both problems emphasize simple state tracking: frequency counts for characters, and relative pointer positions for cycle detection.

🧩 #409 β€” Longest Palindrome

Python (Counter-based)

from collections import Counter

class Solution:
    def longestPalindrome(self, s: str) -> int:
        cnt = Counter(s)
        pairs = sum(v // 2 for v in cnt.values())
        return pairs * 2 + (1 if any(v % 2 for v in cnt.values()) else 0)
Enter fullscreen mode Exit fullscreen mode

C++ (unordered_map)

class Solution {
public:
    int longestPalindrome(string s) {
        unordered_map<char,int> cnt;
        int length = 0;
        bool odd = false;
        for (char c : s) cnt[c]++;
        for (auto &p : cnt) {
            length += (p.second / 2) * 2;
            if (p.second % 2) odd = true;
        }
        return odd ? length + 1 : length;
    }
};
Enter fullscreen mode Exit fullscreen mode

Why it works: all even counts contribute fully, and at most one odd count can add a single center character.

Time: O(n)
Space: O(1) (since only 52 possible letters in standard input)


🧩 #141 β€” Linked List Cycle

Python (Floyd’s Cycle Detection)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        if not head or not head.next:
            return False

        slow = head
        fast = head

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

            if slow == fast:
                return True

        return False
Enter fullscreen mode Exit fullscreen mode

C++ (Floyd’s Cycle Detection)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if (head == nullptr || head->next == nullptr) {
            return false;
        }
        ListNode* slow = head;
        ListNode* fast = head;
        while (fast != nullptr && fast->next != nullptr) {
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast) {
                return true;
            }
        }
        return false;
    }
};
Enter fullscreen mode Exit fullscreen mode

Why it works: two pointers moving at different speeds will eventually meet if there’s a loop, otherwise fast will reach the end.

Time: O(n)
Space: O(1)


πŸ“Έ Achievements

  • Longest Palindrome (Python & C++)

pal py

pal cpp

  • Linked List Cycle (Python & C++)

cycle py

cycle cpp


πŸ“¦ Complexity Recap

  • Longest Palindrome: linear in string length, constant space.
  • Linked List Cycle: linear in list length, constant space.

πŸ“£ Join the Journey

I’m solving and documenting problems daily in both Python and C++, covering hashing, pointers, recursion, and dynamic programming. Follow along if you’re interested in systematic problem solving.

Links

Top comments (0)