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)
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;
}
};
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
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;
}
};
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++)
- Linked List Cycle (Python & C++)
π¦ 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
- LinkedIn: https://www.linkedin.com/in/ertugrul-mutlu
- GitHub Series: https://github.com/Ertugrulmutlu/leetcode-series
Top comments (0)