Lexicographically all Shortest Palindromic Substrings from a given string
Last Updated :
07 Mar, 2022
Given a string s of size N. The task is to find lexicographically all the shortest palindromic substrings from the given string.
Examples:
Input: s= "programming"
Output: a g i m n o p r
Explanation:
The Lexicographical shortest palindrome substring for the word "programming" will be the single characters from the given string. Hence, the output is : a g i m n o p r.
Input: s= "geeksforgeeks"
Output: e f g k o r s
Approach:
To solve the problem mentioned above, the very first observation is that the shortest palindromic substring will be of size 1. So, as per the problem statement, we have to find all distinct substrings of size 1 lexicographically, which means all the characters in the given string.
Below is the implementation of the above approach:
C++
// C++ program to find Lexicographically all
// Shortest Palindromic Substrings from a given string
#include <bits/stdc++.h>
using namespace std;
// Function to find all lexicographically
// shortest palindromic substring
void shortestPalindrome(string s)
{
// Array to keep track of alphabetic characters
int abcd[26] = { 0 };
for (int i = 0; i < s.length(); i++)
abcd[s[i] - 97] = 1;
// Iterate to print all lexicographically shortest substring
for (int i = 0; i < 26; i++) {
if (abcd[i] == 1)
cout << char(i + 97) << " ";
}
}
// Driver code
int main()
{
string s = "geeksforgeeks";
shortestPalindrome(s);
return 0;
}
Java
// Java program to find Lexicographically all
// Shortest Palindromic Substrings from a given string
class Main
{
// Function to find all lexicographically
// shortest palindromic substring
static void shortestPalindrome(String s)
{
// Array to keep track of
// alphabetic characters
int[] abcd = new int[26];
for (int i = 0; i < s.length(); i++)
abcd[s.charAt(i) - 97] = 1;
// Iterate to print all lexicographically
// shortest substring
for (int i = 0; i < 26; i++)
{
if (abcd[i] == 1)
{
System.out.print((char)(i + 97) + " ");
}
}
}
// Driver code
public static void main(String[] args)
{
String s = "geeksforgeeks";
shortestPalindrome(s);
}
}
Python3
# C++ program to find Lexicographically all
# Shortest Palindromic Substrings from a given string
# Function to find all lexicographically
# shortest palindromic substring
def shortestPalindrome (s) :
# Array to keep track of alphabetic characters
abcd = [0]*26
for i in range(len(s)):
abcd[ord(s[i])-97] = 1
# Iterate to print all lexicographically shortest substring
for i in range(26):
if abcd[i]== 1 :
print( chr(i + 97), end =' ' )
# Driver code
s = "geeksforgeeks"
shortestPalindrome (s)
C#
// C# program to find Lexicographically
// all shortest palindromic substrings
// from a given string
using System;
class GFG{
// Function to find all lexicographically
// shortest palindromic substring
static void shortestPalindrome(string s)
{
// Array to keep track of
// alphabetic characters
int[] abcd = new int[26];
for(int i = 0; i < s.Length; i++)
abcd[s[i] - 97] = 1;
// Iterate to print all lexicographically
// shortest substring
for(int i = 0; i < 26; i++)
{
if (abcd[i] == 1)
{
Console.Write((char)(i + 97) + " ");
}
}
}
// Driver code
static public void Main(string[] args)
{
string s = "geeksforgeeks";
shortestPalindrome(s);
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// Javascript program to find Lexicographically all
// Shortest Palindromic Substrings from a given string
// Function to find all lexicographically
// shortest palindromic substring
function shortestPalindrome(s)
{
// Array to keep track of
// alphabetic characters
let abcd = Array.from({length: 26}, (_, i) => 0);
for (let i = 0; i < s.length; i++)
abcd[s[i].charCodeAt() - 97] = 1;
// Iterate to print all lexicographically
// shortest substring
for (let i = 0; i < 26; i++)
{
if (abcd[i] == 1)
{
document.write(String.fromCharCode(i + 97) + " ");
}
}
}
// Driver Code
let s = "geeksforgeeks";
shortestPalindrome(s.split(''));
</script>
Time Complexity: O(N), where N is the size of the string.
Space Complexity: O(1)
Similar Reads
Lexicographically first palindromic string Rearrange the characters of the given string to form a lexicographically first palindromic string. If no such string exists display message "no palindromic string". Examples: Input : malayalam Output : aalmymlaa Input : apple Output : no palindromic string Simple Approach: 1. Sort the string charact
13 min read
Return a Palindromic String after removing minimum length Prefix from given String Given a string B, the task is to find the minimum length prefix of the string which, when removed and added to the end of the string, will make the string a palindrome. Return the palindromic string. If no such string exists, we need to determine that. Examples: Input: "aabb"Output: "abba"Explanatio
5 min read
All distinct palindromic sub-strings of a given string Given a string str of lowercase ASCII characters. The task is to find all the distinct continuous palindromic sub-strings which are present in the string str. Examples: Input: str = "abaaa"Output: [ "a", "aa", "aaa", "aba", "b" ]Explanation: All 5 distinct continuous palindromic sub-strings are list
15+ min read
Count All Palindromic Subsequence in a given String Given a string s of length n, the task is to count number of palindromic subsequence (need not necessarily be distinct) present in the string s.Example: Input: s = "abcd"Output: 4Explanation: Palindromic subsequence are : "a" ,"b", "c" ,"d"Input: s = "aab"Output: 4Explanation: palindromic subsequenc
15+ min read
Count all palindromic Substrings for each character in a given String Given a string S of length n, for each character S[i], the task is to find the number of palindromic substrings of length K such that no substring should contain S[i], the task is to return an array A of length n, where A[i] is the count of palindromic substrings of length K which does not include t
9 min read