Longest Palindromic Substring - Complete Solution Guide
Longest Palindromic Substring is LeetCode problem 5, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a string s , return the longest palindromic substring in s . Example 1: Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer. Example 2: Input: s = "cbbd" Output: "bb" Constraints: 1 <= s.length <= 1000 s consist of only digits and English letters.
Detailed Explanation
The problem asks us to find the longest palindromic substring within a given string 's'. A palindrome is a string that reads the same forwards and backward (e.g., "madam", "racecar"). A substring is a contiguous sequence of characters within a string. We need to return this longest palindromic substring. The input string 's' consists of digits and English letters, and its length is between 1 and 1000.
Solution Approach
The solution employs the 'expand around center' approach. It iterates through each character in the string and considers it as a potential center of a palindrome. For each character, it expands outwards, checking for both odd-length and even-length palindromes. It maintains the start index and maximum length of the longest palindrome found so far. The core idea is that every palindrome has a center (either a single character or the space between two characters), so we can efficiently find palindromes by expanding from each potential center.
Step-by-Step Algorithm
- Step 1: Initialize variables 'start' to 0 (start index of the longest palindrome), and 'max_len' to 1 (minimum length of a palindrome, i.e., a single character).
- Step 2: Iterate through the input string 's' from left to right (index 'i').
- Step 3: For each index 'i', consider it as the center of a potential odd-length palindrome. Initialize 'l' and 'r' to 'i'. Expand outwards while 'l' >= 0 and 'r' < n (string length) and s[l] == s[r]. If a longer palindrome is found, update 'start' and 'max_len'.
- Step 4: For each index 'i', consider it and the next character 'i+1' as the center of a potential even-length palindrome. Initialize 'l' to 'i' and 'r' to 'i+1'. Expand outwards while 'l' >= 0 and 'r' < n and s[l] == s[r]. If a longer palindrome is found, update 'start' and 'max_len'.
- Step 5: After iterating through the entire string, extract the longest palindromic substring using 'start' and 'max_len' (s[start:start + max_len]).
Key Insights
- Insight 1: Palindromes can be centered around a single character (odd length) or between two characters (even length).
- Insight 2: Expanding around the center is an efficient way to find palindromes.
- Insight 3: The optimal approach avoids quadratic space complexity by not storing palindrome lengths in a separate table.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(1)
Topics
This problem involves: Two Pointers, String, Dynamic Programming.
Companies
Asked at: Accenture, Accolite, Adobe, Agoda, Amazon, Apple, Autodesk, BNY Mellon, BlackRock, Bloomberg, ByteDance, CEDCOSS, Cisco, Cognizant, Commvault, Dell, Deloitte, Disney, DoorDash, EPAM Systems, EarnIn, Goldman Sachs, Grab, Huawei, IBM, Info Edge, Infosys, J.P. Morgan, LinkedIn, LiveRamp, MAQ Software, MakeMyTrip, Mastercard, Media.net, Meta, Microsoft, Morgan Stanley, Nielsen, Nutanix, Oracle, Palo Alto Networks, Paytm, PhonePe, Pure Storage, Pwc, RBC, SAP, Salesforce, Samsung, ServiceNow, Softwire, Tesla, ThoughtWorks, TikTok, Tinkoff, Turing, Turo, Uber, UiPath, VMware, Visa, Walmart Labs, Wayfair, Wix, Yahoo, Yandex, Zepto, Zoho, Zopsmart, athenahealth, eBay, persistent systems, tcs.