📜  给定字符串所有最短回文子串的字典序

📅  最后修改于: 2021-09-07 02:02:39             🧑  作者: Mango

给定一个大小为 N 的字符串s 。任务是从给定的字符串字典顺序查找所有最短的回文子字符串。

例子:

方法:
为了解决上述问题,第一个发现是,最短的回文串的大小为1,所以,按照问题的陈述,我们必须找到尺寸1按字典的所有不同子,这意味着在所有的字符给定的字符串。

下面是上述方法的实现:

C++
// C++ program to find Lexicographically all
// Shortest Palindromic Substrings from a given string
 
#include 
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


输出:
e f g k o r s

时间复杂度: O(N),其中 N 是字符串的大小。
空间复杂度: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live