Symmetry in Strings

 Longest Palindromic Substring 

This might seem like a string manipulation task at first, but it's a golden opportunity to understand techniques like brute force exploration, expanding from center, and dynamic programming — all while strengthening your grip on substring logic and indexing.

Let’s explore how to solve this problem using three different approaches: brute force, expand around center, and dynamic programming.

The Best Data Structure for Solving It (And Why!)

When dealing with substrings, two things are crucial:

  • Knowing how to efficiently iterate through all possible substrings.

  • Being able to check if a substring is a palindrome in a smart way.

Brute force gives us the raw idea.
Expand-around-center improves on it with a clever trick.
Dynamic programming brings in structure and memory for optimization.

Let’s dive in!

Different Approaches – From Brute Force to Optimized

1)Brute Force (Naive Approach)

This approach checks all possible substrings and verifies whether they are palindromes. It keeps track of the longest one found.

class Solution { public String longestPalindrome(String s) { int n = s.length(); String longest = ""; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { String sub = s.substring(i, j + 1); if (isPalindrome(sub) && sub.length() > longest.length()) { longest = sub; } } } return longest; } private boolean isPalindrome(String str) { int left = 0, right = str.length() - 1; while (left < right) { if (str.charAt(left++) != str.charAt(right--)) return false; } return true; } }

Explanation:

  • We loop through all substrings of s using two nested loops.

  • For each substring, we check if it’s a palindrome.

  • If it is, and it’s longer than our previous result, we update our result.

2)Expand Around Center (Optimized O(n²))

This approach builds on the fact that a palindrome mirrors around its center. We try to expand from every character (and the gap between characters) as a center and find the maximum length.

class Solution { public String longestPalindrome(String s) { if (s == null || s.length() < 1) return ""; int start = 0, end = 0; for (int i = 0; i < s.length(); i++) { int len1 = expandFromCenter(s, i, i); // Odd length int len2 = expandFromCenter(s, i, i + 1); // Even length int len = Math.max(len1, len2); if (len > end - start) { start = i - (len - 1) / 2; end = i + len / 2; } } return s.substring(start, end + 1); } private int expandFromCenter(String s, int left, int right) { while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { left--; right++; } return right - left - 1; } }

Explanation:

  • We treat each index and gap between characters as a center.

  • Expand as long as the characters at both ends match.

  • Keep track of the longest palindrome found.

3)Dynamic Programming

Here we use a 2D boolean table dp[i][j] to represent whether the substring from index i to j is a palindrome. We build the solution by checking increasing lengths.


class Solution { public String longestPalindrome(String s) { int n = s.length(); boolean[][] dp = new boolean[n][n]; String result = ""; for (int l = 0; l < n; l++) { for (int i = 0; i < n - l; i++) { int j = i + l; if (s.charAt(i) == s.charAt(j) && (l < 2 || dp[i + 1][j - 1])) { dp[i][j] = true; if (l + 1 > result.length()) { result = s.substring(i, j + 1); } } } } return result; } }

Explanation:

  • We start with substrings of length 1 and build up.

  • If characters at the ends match and the inside is a palindrome, the whole thing is.

  • Track and return the longest one found.

⚠️ Common Mistakes & Pitfalls to Avoid

  • Forgetting Even-Length Palindromes: Don't just expand around single characters — check between them too!

  • Off-by-One Errors: Especially with substring indices in Java — remember it's exclusive at the end.

  • Inefficient Checking: In brute force, checking every substring without memoization becomes slow very quickly.

📊 Time and Space Complexity Analysis

Brute Force:

  • Time: O(n³) — generating substrings and checking if they’re palindromes

  • Space: O(1) if you don’t count the result string

Expand Around Center:

  • Time: O(n²)

  • Space: O(1)

Dynamic Programming:

  • Time: O(n²)

  • Space: O(n²) for the DP table

🌍 Real-World Applications

Finding palindromic substrings isn't just academic — it shows up in:

  • DNA Analysis: Where palindromic sequences have biological significance

  • Data Compression: Where repeated patterns are exploited

  • Text Processing: For finding symmetries or patterns in documents and logs


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries