Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Longest Palindrome in String Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Longest Palindrome in String | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a string s, find the longest palindrome substring in s. If there are multiple valid substrings, find the first one.

Naive Approach

The simple solution is to check each substring whether it is palindrome or not and find the longest palindrome among them.

To do that, run two nested loop to decide the start and end of each loop. Now run a third nested loop from start to end to check whether the string is palindrome or not.

If the string is a palindrome, store the starting index and string length for the longest palindrome among them.

Analysis

  • Time Complexity: O(n3)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
string getLongestPalindrome(string s) {
   int n = s.size();
   int index  = -1, palindromeLength = 0;
   for (int i = 0; i < n; i++) {
       for (int j = i; j < n; j++) {
           int isPalindrome = 1;
           for (int k = i; k <= j; k++) {
               if (s[k] != s[j - (k - i)]) {
                   isPalindrome = 0;
               }
           }
           if (isPalindrome == 1 && j - i + 1 > palindromeLength) {
               index = i;
               palindromeLength = j - i + 1;
           }
       }
   }
   string ans = "";
   for (int i = index; i < index + palindromeLength; i++) {
       ans += s[i];
   }
   return ans;
}
Java
class Solution {
   String getLongestPalindrome(String s) {
       int n = s.length();
       int index  = -1, palindromeLength = 0;
       for (int i = 0; i < n; i++) {
           for (int j = i; j < n; j++) {
               int isPalindrome = 1;
               for (int k = i; k <= j; k++) {
                   if (s.charAt(k) != s.charAt(j - (k - i))) {
                       isPalindrome = 0;
                   }
               }
               if (isPalindrome == 1 && j - i + 1 > palindromeLength) {
                   index = i;
                   palindromeLength = j - i + 1;
               }
           }
       }
       String ans = "";
       for (int i = index; i < index + palindromeLength; i++) {
           ans += s.charAt(i);
       }
       return ans;
   }
}

Optimal Approach

The optimal approach for this problem is quite similar to the naive approach. Here instead of deciding the start index will decide the middle index for a palindromic string.

  • Traverse the string and consider each index as the middle point of the palindromic string. Here two different cases will be encountered.
  • Case 1: Palindromic string has even length
    • In this case, assign two pointers left = i - 1 and right  = i as centre and expand left and right in both directions while s[left] == s[right]. Keep updating the starting index and the maximum length of the palindromic string.
  • Case 2: Palindromic string has odd length
    • In this case, the two pointers will be assigned as left = i -1 nd right = i + 1 as index i is the middle element. Now repeat the same process as of the even length string.
  • Return the palindromic substring having the maximum length.

Analysis

  • Time Complexity: O(n2)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
string getLongestPalindrome(string s) {
   int n = s.size();
   int index  = 0, palindromeLength = 1;
   for (int i = 1; i < n; i++) {
       int left = i - 1, right = i;
       while(left >= 0 && right < n && s[left] == s[right]) {
           if(right - left + 1 > palindromeLength) {
               index = left;
               palindromeLength = right - left + 1;
           }
           left--;
           right++;
       }
       left = i - 1;
       right = i + 1;
       while(left >= 0 && right < n && s[left] == s[right]) {
           if(right - left + 1 > palindromeLength) {
               index = left;
               palindromeLength = right - left + 1;
           }
           left--;
           right++;
       }
   }
   string ans = "";
   for (int i = index; i < index + palindromeLength; i++) {
       ans += s[i];
   }
   return ans;
}
Java
class Solution {
   String getLongestPalindrome(String s) {
       int n = s.length();
       int index  = 0, palindromeLength = 1;
       for (int i = 1; i < n; i++) {
           int left = i - 1, right = i;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           left = i - 1;
           right = i + 1;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           
       }
       String ans = "";
       for (int i = index; i < index + palindromeLength; i++) {
           ans += s.charAt(i);
       }
       return ans;
   }
}
Related Content
Anagrams
Compare Version Numbers
Count And Say
Integer to Roman Numeral
Longest Common Prefix
Insert Minimum To Make Palindrome
Reverse Words in String
Roman Numeral to Integer
Substring Search - I
Substring Search - III
Substring Search - II
Substring Search - IV
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet