Doing a coding challenge LIVE: Longest Palindromic Substring!
Автор: KG.codes
Загружено: 2025-12-05
Просмотров: 150
Описание:
Here is the problem and solution to a coding challenge from Leetcode.com called Longest Palindromic Substring!
Given a string s, return the longest palindromic substring in s.
Example
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Intuition / tip:
Every palindrome expands outward from its center. By treating each character (and the space between characters) as a possible center, you can grow outward to find the longest odd and even length palindromes.
Solution steps:
1. Initialize the longest palindrome as the first character.
2. Loop through each index in the string as a possible center.
3. Expand outward from the center to find the longest odd-length palindrome.
4. Expand outward from the center and the next index to find the longest even-length palindrome.
5. Compare both results to the current longest palindrome and update if needed.
6. Return the longest palindrome found after all centers are checked.
Time complexity:
O(n²), because for each character in the string, the algorithm may expand across the entire string in the worst case.
Space complexity:
O(1), because only a constant amount of extra space is used regardless of input size.
💬 Comment: Let me know if you have any questions or see ways I could have optimized!
If you found this interesting or helpful, please consider liking, sharing, or saving to support it.
Let's connect!
🌐 Website | https://www.kg.codes
📺 Subscribe | / @kgcodes
📷 Instagram | www.instagram.com/kg.codes/
🎵 TikTok | www.tiktok.com/@kg.codes
🐦 Twitter | www.twitter.com/kgcodes/
Get my gear / desk setup / smart home 👨🏾💻
📦 Amazon | https://www.amazon.com/shop/kg.codes/...
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: