The goal is simple but interesting: given a string s, find the longest substring that reads the same forward and backward.
For example:
-
Input:
"babad" -
Output:
"bab"or"aba"
💡 Approach: Expand Around Center
Instead of generating every possible substring and checking whether it is a palindrome, I used the Expand Around Center technique.
Every palindrome has a center:
- Odd-length palindrome → one character is the center.
- Even-length palindrome → the gap between two characters is the center.
For every character, we check both possibilities and expand outward while the characters are equal
📊 Complexity
The solution takes O(n²) time in the worst case because we expand around each possible center.
The extra space complexity is O(1), making it much more memory-efficient than approaches that store all substrings or use a large dynamic-programming table.
This problem was a great reminder that sometimes the key to optimizing a solution is not complicated code—it’s recognizing the structure of the problem.
I’m continuing to practice Data Structures & Algorithms and improve my problem-solving skills one challenge at a time. 💻
What approach would you use for this problem—Expand Around Center, Dynamic Programming, or Manacher’s Algorithm?
#JavaScript #LeetCode #Coding #Programming #DSA #DataStructures #Algorithms #ProblemSolving #WebDevelopment #FrontendDevelopment #100DaysOfCode #CodingJourney #SoftwareDevelopment #Tech #Developer #JavaScriptDeveloper