本文概述
- C ++
- Java
- Python3
- C#
例子:
输入:s ="programming"方法:
输出:a g i n o p r
说明:单词"programming"的词典学上最短回文子字符串将是给定字符串中的单个字符。因此, 输出为:a g i m n o p r。
输入:s ="lsbin"
输出:e f g k o r s
为了解决上述问题, 最先观察到的是, 最短回文子字符串的大小为1。因此, 根据问题陈述, 我们必须按字典顺序找到大小为1的所有不同子字符串, 这意味着给定字符串中的所有字符。
下面是上述方法的实现:
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展开 - 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 = "lsbin" ;
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展开 = 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 = "lsbin" ;
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 ] * 26for 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 = "lsbin"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展开 - 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 = "lsbin" ;
shortestPalindrome(s);
}
} //This code is contributed by AnkitRai01
输出如下:
e f g k o r s
时间复杂度:O(N), 其中N是字符串的大小。
【按字典顺序,给定字符串的所有最短回文子字符串】空间复杂度:O(1)
推荐阅读
- Java使用正则表达式验证日期格式
- 打印数组A[]中的所有字符串,并将数组B[]中的所有字符串作为子序列
- CRASH()宏–用法解释
- 如何创建和访问Python包(详细示例)
- 创建具有O和X的交替矩形的矩阵
- 使用tkinter在Python中创建是/否消息框
- 自激活W10纯净版 雨林木风64位WIN8.1专业版推荐
- 雨林木风纯净专业版Win8.1系统推荐
- 免激活W8 Win8之家Win8.1 64位专业版推荐下载