Posts

Longest Palindromic Substring

Image
Problem Statement Given a string s, find and return the longest palindromic substring in s . If there are multiple answers, return any one.  Example 1 : dabac Answer: aba Example 2: abc Answer: a Note: 'a', 'b' and 'c' are all  answers, and we can return any of the 3 answers.  Leet Code :  https://leetcode.com/problems/longest-palindromic-substring/  Approaches for solving this problem  1) Brute force approach  From the string s, generate all possible substrings. This would need a time of O(n^2). We then need to validate if each of these substrings are palindromes or not.  Checking if a string is a palindrome would need a time of O(n). Now if we figure that the substring which we checked is a palindrome, we need to check if that is the palindrome with the maximum length we have seen so far. If so, we will update the maxPalindrome with this one. We will repeat the process for all substrings and at the end of it we will have the maxPalindrome which we can return b