Given a string you need to find the shortest palindromic substring of the string. If there are multiple answers output the lexicographically smallest.
Examples:
Input: zyzz Output:y Input: abab Output: a
Naive Approach:
- The approach is similar to finding the longest palindromic substring. We keep track of even and odd lengths substring and keep storing it in a vector.
- After that, we will sort the vector and print the lexicographically smallest substring. This may also include empty substrings but we need to ignore them.
Below is the implementation of the above approach:
// C++ program to find the shortest // palindromic substring #include <bits/stdc++.h> using namespace std; // Function return the shortest // palindromic substring string ShortestPalindrome(string s) { int n = s.length(); vector<string> v; // One by one consider every character // as center point of even and length // palindromes for (int i = 0; i < n; i++) { int l = i; int r = i; string ans1 = ""; string ans2 = ""; // Find the longest odd length palindrome // with center point as i while (l >= 0 && r < n && s[l] == s[r]) { ans1 += s[l]; l--; r++; } l = i - 1; r = i; // Find the even length palindrome // with center points as i-1 and i. while (l >= 0 && r < n && s[l] == s[r]) { ans2 += s[l]; l--; r++; } v.push_back(ans1); v.push_back(ans2); } string ans = v[0]; // Smallest substring which is // not empty for (int i = 0; i < v.size(); i++) { if (v[i] != "") { ans = min(ans, v[i]); } } return ans; } // Driver code int main() { string s = "geeksforgeeks"; cout << ShortestPalindrome(s); return 0; } |
e
Time complexity: O(N^2), where N is the length of the string.
Efficient Approach:
- An observation here is that a single character is also a palindrome. So, we just need to print the lexicographically smallest character present in the string.
Below is the implementation of the above approach:
C++
// C++ program to find the shortest // palindromic substring #include <bits/stdc++.h> using namespace std; // Function return the shortest // palindromic substring char ShortestPalindrome(string s) { int n = s.length(); char ans = s[0]; // Finding the smallest character // present in the string for(int i = 1; i < n ; i++) { ans = min(ans, s[i]); } return ans; } // Driver code int main() { string s = "geeksforgeeks"; cout << ShortestPalindrome(s); return 0; } |
Java
// Java program to find the shortest // palindromic subString class GFG{ // Function return the shortest // palindromic subString static char ShortestPalindrome(String s) { int n = s.length(); char ans = s.charAt(0); // Finding the smallest character // present in the String for(int i = 1; i < n; i++) { ans = (char) Math.min(ans, s.charAt(i)); } return ans; } // Driver code public static void main(String[] args) { String s = "geeksforgeeks"; System.out.print(ShortestPalindrome(s)); } } // This code is contributed by Rajput-Ji |
C#
// C# program to find the shortest // palindromic subString using System; class GFG{ // Function return the shortest // palindromic subString static char ShortestPalindrome(String s) { int n = s.Length; char ans = s[0]; // Finding the smallest character // present in the String for(int i = 1; i < n; i++) { ans = (char) Math.Min(ans, s[i]); } return ans; } // Driver code public static void Main(String[] args) { String s = "geeksforgeeks"; Console.Write(ShortestPalindrome(s)); } } // This code is contributed by 29AjayKumar |
e
Time complexity: O(N), where N is the length of the string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum length of substring whose rotation generates a palindromic substring
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Lexicographically all Shortest Palindromic Substrings from a given string
- Shortest substring of a string containing all given words
- Longest Palindromic Substring | Set 2
- Longest Palindromic Substring | Set 1
- Longest Non-palindromic substring
- Longest palindromic string possible after removal of a substring
- Check if a substring can be Palindromic by replacing K characters for Q queries
- Suffix Tree Application 6 - Longest Palindromic Substring
- Maximum length palindromic substring such that it starts and ends with given char
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Make palindromic string non-palindromic by rearranging its letters
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

