Longest Palindromic Substring in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to find the longest palindromic substring grows as the input string gets longer.
How does the number of steps change when the string size increases?
Analyze the time complexity of the following code snippet.
function longestPalindrome(s: string): string {
let start = 0, maxLength = 1;
for (let i = 0; i < s.length; i++) {
let left = i, right = i;
while (left >= 0 && right < s.length && s[left] === s[right]) {
if (right - left + 1 > maxLength) {
start = left;
maxLength = right - left + 1;
}
left--;
right++;
}
}
return s.substring(start, start + maxLength);
}
This code finds the longest odd-length palindrome by expanding around each character as the center.
- Primary operation: The inner while loop expands around each center to check for palindromes.
- How many times: The outer for loop runs once per character (n times), and the inner while loop can run up to n times in the worst case.
As the string length grows, the number of checks grows roughly with the square of the length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 checks |
| 100 | About 10,000 checks |
| 1000 | About 1,000,000 checks |
Pattern observation: Doubling the input size roughly quadruples the work needed.
Time Complexity: O(n²)
This means the time to find the longest palindrome grows roughly with the square of the string length.
[X] Wrong: "The algorithm runs in linear time because it only loops through the string once."
[OK] Correct: Although the outer loop runs once per character, the inner loop can expand many times, making the total work much larger than just one pass.
Understanding this time complexity helps you explain how your solution scales and shows you can analyze nested loops clearly.
"What if we also expand around pairs of characters to handle even-length palindromes? How would the time complexity change?"
Practice
Longest Palindromic Substring problem?Solution
Step 1: Understand palindrome definition
A palindrome reads the same forwards and backwards, like 'madam' or 'racecar'.Step 2: Identify the problem goal
The problem asks to find the longest substring inside a string that is a palindrome.Final Answer:
Find the longest substring that reads the same forwards and backwards -> Option BQuick Check:
Palindrome substring = longest palindrome [OK]
- Confusing palindrome with unique characters
- Looking for longest substring without repeats
- Thinking about substrings with vowels only
Solution
Step 1: Check input parameter type
The input is a string, so parameter type must bestring.Step 2: Check return type
The function returns the longest palindromic substring, which is a string.Final Answer:
function longestPalindrome(s: string): string -> Option AQuick Check:
Input string, output string [OK]
- Using wrong input type like number or boolean
- Returning array instead of string
- Mixing input and output types
function longestPalindrome(s: string): string {
let start = 0, maxLength = 1;
for (let i = 0; i < s.length; i++) {
let low = i, high = i;
while (low >= 0 && high < s.length && s[low] === s[high]) {
if (high - low + 1 > maxLength) {
start = low;
maxLength = high - low + 1;
}
low--;
high++;
}
}
return s.substring(start, start + maxLength);
}
console.log(longestPalindrome('babad'))Solution
Step 1: Understand the code logic
The code expands around each character to find odd-length palindromes and tracks the longest one.Step 2: Trace the input 'babad'
Expanding around index 1 ('a') finds 'bab' (length 3). Around index 2 ('b') finds 'aba' (length 3). Both have length 3, but 'bab' is found first and stored.Final Answer:
"bab" -> Option AQuick Check:
First longest palindrome found = "bab" [OK]
- Assuming 'aba' is always returned instead of 'bab'
- Returning entire string incorrectly
- Confusing substring indices
function longestPalindrome(s: string): string {
let start = 0, maxLength = 1;
for (let i = 0; i < s.length; i++) {
let low = i, high = i + 1;
while (low >= 0 && high < s.length && s[low] === s[high]) {
if (high - low + 1 > maxLength) {
start = low;
maxLength = high - low + 1;
}
low--;
high++;
}
}
return s.substring(start, start + maxLength);
}Solution
Step 1: Analyze initialization of low and high
low = i and high = i + 1 means it only checks even-length palindromes centered between i and i+1.Step 2: Identify missing odd-length palindrome check
Odd-length palindromes centered at i (low = high = i) are not checked, so some palindromes are missed.Final Answer:
The code only checks even-length palindromes, missing odd-length ones -> Option CQuick Check:
Only even-length palindrome check = bug [OK]
- Checking only even-length palindromes
- Not expanding around single character centers
- Incorrect loop start index
"forgeeksskeegfor", which approach correctly finds the longest palindromic substring and what is that substring?Solution
Step 1: Identify the longest palindrome in the string
The substring "geeksskeeg" is the longest palindrome inside "forgeeksskeegfor".Step 2: Match approach with correct palindrome
Expand around center approach finds "geeksskeeg" correctly. The entire string is not a palindrome, so options A and B are incorrect. Brute force (C) incorrectly identifies "ss" as the longest.Final Answer:
Expand around center approach; longest palindrome is "geeksskeeg" -> Option DQuick Check:
Center expansion finds "geeksskeeg" [OK]
- Assuming entire string is palindrome
- Confusing brute force with efficient methods
- Mixing up palindrome substrings
Ready to prove it?
Pass the test and this pattern moves unknown
