Open In App

Lexicographically all Shortest Palindromic Substrings from a given string

Last Updated : 07 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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>


Output: 

e f g k o r s

 

Time Complexity: O(N), where N is the size of the string.
Space Complexity: O(1)
 



Similar Reads

Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
Given a binary string str of length N, and an integer K, the task is to find the lexicographically smallest permutation of the string str that can be reduced to length K by removal of every K-length prefix from palindromic substrings of length 2K. If no such permutation exists, print "Not Possible". Examples: Input: str = "0000100001100001”, K = 4O
14 min read
Lexicographically shortest string of length at most K which is not a substring of given String
Given a string S, the task is to find the lexicographically shortest string of length less than or equal to K which is not a substring of the given string. If not possible, print -1. Examples: Input: S = zxabcehgf, K = 2Output: dExplanation: Lexicographically, the shortest string which is not a substring of a given string is d. Input: S = sdhaacbde
9 min read
Lexicographically smallest permutation of a string that contains all substrings of another string
Given two strings A and B, the task is to find lexicographically the smallest permutation of string B such that it contains every substring from the string A as its substring. Print “-1” if no such valid arrangement is possible.Examples: Input: A = "aa", B = "ababab" Output: aaabbb Explanation: All possible substrings of A are ('a', 'a', 'aa') Rear
10 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 the character S[i]. Examples: Input: S = "aababba",
9 min read
Minimum cuts required to convert a palindromic string to a different palindromic string
Given palindromic string s, the task is to find minimum k, such that you can cut this string into k+1 parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string s. If it is impossible then print -1.Examples: Input : string = "civic" Output : 2 Explanation : ci | v | ic --&gt; ic |
16 min read
Find all words in given Sentence that are lexicographically increasing and lexicographically decreasing
Given a string representing a sentence named str of size N, the task is to find all the valid words in a sentence that are lexicographically sorted in increasing order and in decreasing order along with their counts. Note: Valid words are:- words don't containing numbers.words greater than size 1. Examples: Input: str="geeks for geeks"Output: 1 "fo
10 min read
Minimum characters to be replaced in Ternary string to remove all palindromic substrings for Q queries
Given a ternary string S of length N containing only '0', '1' and '2' characters and Q queries containing a range of indices [L, R], the task for each query [L, R] is to find the minimum number of characters to convert to either '0', '1' or '2' such that there exists no palindromic substring of length at least 2, between str[L] and str[R]. Examples
11 min read
Lexicographically smallest string formed by reversing Substrings of string S exactly K times
Given a string S and an integer K, the task is to find the lexicographically smallest string possible after reversing any substring of any length exactly K times. Examples: Input: S = "fgazcbdfge", K = 3Output: abcdgfzfgeExplanation: After 1st operation: S = "agfzcbdfge", in S select S[0 - 2] = "fga" and reverse itAfter 2nd operation: S = "abczfgdf
11 min read
Permutation of given string that maximizes count of Palindromic substrings
Given a string S, the task is to find the permutation of the string such that palindromic substrings in the string are maximum.Note: There can be multiple answers for each string. Examples: Input: S = "abcb" Output: "abbc" Explanation: "abbc" is the string with maximum number of palindromic substrings. Palindromic Substrings are - {"a", "b", "b", "
3 min read
Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
Given two strings str1 and str2, the task is to check if it is possible to form a Palindromic String by concatenation of two substrings of str1 and str2. Examples: Input: str1 = "abcd", str2 = "acba"Output: YesExplanation:There are five possible cases where concatenation of two substrings from str1 and str2 gives palindromic string:"ab" + "a" = "ab
6 min read