Illustration of string prefixes

Longest Common Prefix: Five Algorithms Compared

🔢 Problem Statement LeetCode 14 – Longest Common Prefix Given an array of strings, return the longest common prefix among them. If no common prefix exists, return an empty string (""). Example 1 Input: ["flower", "flow", "flight"] → Output: "fl" Example 2 Input: ["dog", "racecar", "car"] → Output: "" Constraints 1 ≤ strs.length ≤ 200 0 ≤ strs[i].length ≤ 200 strs[i] contains only lowercase English letters if non-empty. 👨‍💻 Solution Strategies We’ll explore and compare five distinct approaches, from brute force to divide & conquer: ...

August 3, 2025 · 3 min · Frederico Gago
Python Code Cover Image

Roman to Integer — Clean, Fast, and Functional Approaches Compared

🧩 Problem: Roman to Integer (LeetCode 13) Convert a string representing a Roman numeral into its integer value. Roman numerals are typically written from largest to smallest (left to right), except for six well-known subtractive cases: I before V or X → 4, 9 X before L or C → 40, 90 C before D or M → 400, 900 ✅ Clean Idiomatic Solution (One-Pass) class Solution: SYMBOLS = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 } def romanToInt(self, s: str) -> int: total = 0 prev_value = 0 for c in s: value = self.SYMBOLS[c] total += value - 2 * prev_value if value > prev_value else value prev_value = value return total Single loop Detects subtractive patterns with value > prev_value Clean inline logic ✅ Runtime: 3 ms (faster than 79.17%) ✅ Memory: 17.6 MB (less than 90.19%) ...

July 20, 2025 · 3 min · Frederico Gago
Python Code Cover Image

Is It a Palindrome Number? Three Ways to Solve It Without Converting to String

🧩 The Problem Given an integer x, determine whether it’s a palindrome — that is, whether it reads the same forwards and backwards. 🔗 Problem Link: LeetCode 9. Palindrome Number Examples Input: x = 121 → Output: True Input: x = -121 → Output: False Input: x = 10 → Output: False Bonus: Can you solve it without converting the integer to a string? 🔍 The Three Approaches I Explored 1. 💡 Direct Solution with String Conversion def isPalindrome(x: int) -> bool: return str(x) == str(x)[::-1] Simple and fast (4 ms on LeetCode) But does not satisfy the follow-up constraint of avoiding strings 2. 📐 Mathematical Reversal Using log10 and Closed-Form Formula Inspired by this StackExchange post, this solution uses a formula to reverse digits without strings: ...

July 6, 2025 · 2 min · Frederico Gago