Longest Palindromic Substring in DSA C - Time & Space Complexity
We want to know how the time needed to find the longest palindromic substring changes as the input string gets longer.
How does the number of steps grow when the string size increases?
Analyze the time complexity of the following code snippet.
int expandAroundCenter(char *s, int left, int right, int n) {
int L = left, R = right;
while (L >= 0 && R < n && s[L] == s[R]) {
L--;
R++;
}
return R - L - 1;
}
char* longestPalindrome(char * s) {
int start = 0, end = 0;
int len = strlen(s);
for (int i = 0; i < len; i++) {
int len1 = expandAroundCenter(s, i, i, len);
int len2 = expandAroundCenter(s, i, i + 1, len);
int maxLen = (len1 > len2) ? len1 : len2;
if (maxLen > end - start) {
start = i - (maxLen - 1) / 2;
end = i + maxLen / 2;
}
}
s[end + 1] = '\0';
return s + start;
}
This code finds the longest palindromic substring by expanding around each character and its neighbor.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop inside
expandAroundCenterthat expands outwards checking characters. - How many times: For each character in the string (n times), we call this expansion twice, each expanding up to n steps 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 done.
Time Complexity: O(n²)
This means the time needed grows roughly with the square of the input size.
[X] Wrong: "The expansion around center runs in linear time overall because it stops early."
[OK] Correct: While each expansion stops early for many centers, in the worst case (like a string of identical characters), expansions can reach the full length, making total work quadratic.
Understanding this time complexity helps you explain why this approach is efficient enough for many cases and when to consider faster algorithms.
"What if we used dynamic programming to store palindrome checks instead of expanding around centers? 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 substring requirement
The substring must be continuous characters in the string, not scattered.Final Answer:
To find the longest continuous sequence of characters that reads the same backward and forward -> Option CQuick Check:
Longest palindromic substring = continuous palindrome [OK]
- Confusing substring with subsequence
- Looking for unique characters instead of palindrome
- Ignoring continuity of characters
Solution
Step 1: Check correct function for string length in C
In C,strlen()is used to get string length, notlength()ors.length.Step 2: Verify variable initialization
Longest palindrome length starts at 1 (single char), start index at 0, and length is string length.Final Answer:
int maxLength = 1; int start = 0; int len = strlen(s); -> Option BQuick Check:
Use strlen() for string length in C [OK]
- Using length() instead of strlen() in C
- Initializing maxLength to 0 instead of 1
- Using Java or C++ string length syntax in C
char s[] = "babad";
int maxLength = 1, start = 0;
int len = strlen(s);
for (int i = 0; i < len; i++) {
int low = i - 1;
int high = i + 1;
while (high < len && s[high] == s[i])
high++;
while (low >= 0 && s[low] == s[i])
low--;
while (low >= 0 && high < len && s[low] == s[high]) {
low--;
high++;
}
int length = high - low - 1;
if (maxLength < length) {
maxLength = length;
start = low + 1;
}
}
printf("%d", maxLength);Solution
Step 1: Understand the palindrome expansion logic
The code expands around each character to find palindromes, including duplicates and symmetric characters.Step 2: Trace the longest palindrome in "babad"
Longest palindromic substrings are "bab" or "aba", both length 3.Final Answer:
3 -> Option AQuick Check:
Longest palindrome in "babad" = 3 [OK]
- Assuming entire string is palindrome
- Counting non-continuous characters
- Ignoring multiple palindrome centers
char s[] = "cbbd";
int maxLength = 1, start = 0;
int len = strlen(s);
for (int i = 0; i < len; i++) {
int low = i - 1;
int high = i + 1;
while (low >= 0 && high < len && s[low] == s[high]) {
low--;
high++;
}
int length = high - low - 1;
if (maxLength < length) {
maxLength = length;
start = low + 1;
}
}
printf("%d", maxLength);Solution
Step 1: Analyze palindrome detection logic
The code expands around a single center character, missing even length palindromes like "bb" in "cbbd".Step 2: Identify missing even length palindrome check
Even length palindromes require checking centers between characters, which is not done here.Final Answer:
The code does not check for even length palindromes correctly -> Option AQuick Check:
Missing even length palindrome check = bug [OK]
- Only checking odd length palindromes
- Not updating start index correctly
- Wrong initial maxLength value
"forgeeksskeegfor", which approach correctly finds the longest palindromic substring and what is its length?
A) Brute force method, length 8
B) Dynamic programming method, length 12
C) Expand around center method, length 12
D) Expand around center method, length 10Solution
Step 1: Identify longest palindrome in "forgeeksskeegfor"
The longest palindromic substring is "geeksskeeg", length 10.Step 2: Check method correctness and length
Expand around center method can find the longest palindrome correctly. The longest palindrome is "geeksskeeg" (length 10).Step 3: Re-examine options
Expand around center method, length 10 says expand around center method, length 10 which is correct. Other options have incorrect lengths or methods.Final Answer:
Expand around center method, length 10 -> Option DQuick Check:
Longest palindrome "geeksskeeg" length 10 [OK]
- Assuming full string is palindrome
- Confusing length 12 with actual palindrome length
- Mixing method with incorrect length
Ready to prove it?
Pass the test and this pattern moves unknown
