Longest Palindromic Substring in DSA Go - 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.
func longestPalindrome(s string) string {
n := len(s)
if n == 0 {
return ""
}
start, maxLen := 0, 1
for i := 0; i < n; i++ {
expandAroundCenter := func(left, right int) {
for left >= 0 && right < n && s[left] == s[right] {
if right-left+1 > maxLen {
start = left
maxLen = right - left + 1
}
left--
right++
}
}
expandAroundCenter(i, i) // odd length palindrome
expandAroundCenter(i, i+1) // even length palindrome
}
return s[start : start+maxLen]
}
This code checks every character as a center and expands outwards to find palindromes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Expanding around each center to check palindrome characters.
- How many times: For each of the n characters, expansion can happen up to n times in worst case.
As the string length grows, the number of expansions grows roughly with the square of the length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 expansions |
| 100 | About 10,000 expansions |
| 1000 | About 1,000,000 expansions |
Pattern observation: Doubling the input size roughly quadruples the work.
Time Complexity: O(n²)
This means the time needed grows roughly with the square of the input size, so longer strings take much more time.
[X] Wrong: "Checking palindromes around each center is just O(n) because we only loop once."
[OK] Correct: Each center can expand up to n times, so the total work is much more than a single loop.
Understanding this time complexity helps you explain why some palindrome solutions are slower and shows your grasp of nested loops and string processing.
"What if we used dynamic programming to store palindrome checks? How would the time complexity change?"
Practice
Longest Palindromic Substring in a given string?Solution
Step 1: Understand palindrome definition
A palindrome reads the same backward and forward, like 'madam' or 'racecar'.Step 2: Identify longest continuous palindrome
The problem asks for the longest continuous substring that is a palindrome, not just any substring or character pattern.Final Answer:
To find the longest continuous sequence of characters that reads the same backward and forward -> Option AQuick Check:
Longest palindromic substring = continuous palindrome [OK]
- Confusing substring with subsequence
- Looking for unique characters instead of palindrome
- Ignoring continuity of substring
s?Solution
Step 1: Check input parameter type
The input should be a string to find its palindromic substring, so parameter type must bestring.Step 2: Check return type
The function returns the longest palindromic substring, which is a string, so return type must bestring.Final Answer:
func longestPalindrome(s string) string -> Option BQuick Check:
Input string, output string [OK]
- Using wrong input type like int
- Returning int instead of string
- Missing input parameter
package main
import "fmt"
func longestPalindrome(s string) string {
start, maxLen := 0, 1
for i := 0; i < len(s); i++ {
for j := i; j < len(s); j++ {
sub := s[i : j+1]
if isPalindrome(sub) && len(sub) > maxLen {
start = i
maxLen = len(sub)
}
}
}
return s[start : start+maxLen]
}
func isPalindrome(str string) bool {
for i := 0; i < len(str)/2; i++ {
if str[i] != str[len(str)-1-i] {
return false
}
}
return true
}
func main() {
fmt.Println(longestPalindrome("babad"))
}Solution
Step 1: Understand the code logic
The code checks all substrings and updates the longest palindrome found. For "babad", "bab" and "aba" are both palindromes of length 3.Step 2: Check which palindrome is returned
The code updates start and maxLen when a longer palindrome is found. It finds "bab" first and keeps it because "aba" comes later but has the same length, so "bab" remains.Final Answer:
"bab" -> Option AQuick Check:
First longest palindrome found = "bab" [OK]
- Assuming "aba" is returned instead of "bab"
- Thinking entire string is palindrome
- Ignoring substring length comparison
func longestPalindrome(s string) string {
start, maxLen := 0, 1
for i := 0; i < len(s); i++ {
for j := i; j < len(s); j++ {
sub := s[i:j]
if isPalindrome(sub) && len(sub) > maxLen {
start = i
maxLen = len(sub)
}
}
}
return s[start : start+maxLen]
}Solution
Step 1: Check substring slicing
In Go, s[i:j] excludes character at index j, so substring misses last character.Step 2: Correct slicing to include last character
Changing to s[i:j+1] includes character at j, ensuring full substring is checked.Final Answer:
The substring slice should be s[i:j+1] to include character at index j -> Option DQuick Check:
Substring slicing excludes last char if j not incremented [OK]
- Using s[i:j] misses last character
- Not adjusting loop indices accordingly
- Assuming isPalindrome is missing
s = "forgeeksskeegfor", which approach below efficiently finds the longest palindromic substring and what is that substring?Solution
Step 1: Identify efficient methods
Dynamic programming stores palindrome info to avoid repeated checks, efficient for this problem.Step 2: Find longest palindrome in given string
"geeksskeeg" is the longest palindromic substring inside "forgeeksskeegfor"; entire string is not palindrome.Final Answer:
Dynamic programming with memoization; longest palindrome is "geeksskeeg" -> Option CQuick Check:
DP finds "geeksskeeg" efficiently [OK]
- Assuming entire string is palindrome
- Using brute force which is slow
- Sorting characters breaks substring continuity
Ready to prove it?
Pass the test and this pattern moves unknown
