给定两个intgers氮钾(K≤N),该任务是获得长度N此字符串的一个回文子的,使得最大长度的字符串为K。
例子:
Input: N = 5, K = 3
Output: “abacd”
Explanation: Palindromic substrings are “a”, “b”, “c”, “d” and “aba”. Therefore, the longest palindromic substring from the given string is of length 3.
Input: N = 8, K = 4
Output: “abbacdef”
Explanation: Palindromic subtsrings are “a”, “b”, “c”, “d”, “e”, “f”, “bb”, “abba”. Therefore, the longest palindromic substring from the given string is of length 4.
方法:该想法基于以下观察结果:由单个字符组成的任意长度的字符串始终是回文的,例如{‘a’,’bbbbb’,’ccc’}。因此,为了生成具有所需条件的字符串,请打印“ a” K次,以使其具有最长长度为K的回文子字符串,并以非回文序列填充其余的N – K个插槽。
请按照以下步骤解决问题:
- 精确地打印“ a” K次。
- 考虑一个非回文序列,例如“ bcd”。
- 打印字符串。
下面是上述方法的实现:
C++
// C++ program to implement the above approach
#include
using namespace std;
// Function to generate a string of
// length N having longest palindromic
// substring of length K
void string_palindrome(int N, int K)
{
// Fill first K characters with 'a'
for (int i = 0; i < K; i++)
cout << "a";
// Stores a non-palindromic sequence
// to be repeated for N - k slots
string s = "bcd";
// Print N - k remaining characters
for (int i = 0; i < N - K; i++)
cout << s[i % 3];
}
// Driver Code
int main()
{
// Given N and K
int N = 5, K = 3;
string_palindrome(N, K);
return 0;
}
Java
// Java program to implement the above approach
import java.util.*;
class GFG
{
// Function to generate a String of
// length N having longest palindromic
// subString of length K
static void String_palindrome(int N, int K)
{
// Fill first K characters with 'a'
for (int i = 0; i < K; i++)
System.out.print("a");
// Stores a non-palindromic sequence
// to be repeated for N - k slots
String s = "bcd";
// Print N - k remaining characters
for (int i = 0; i < N - K; i++)
System.out.print(s.charAt(i % 3));
}
// Driver Code
public static void main(String[] args)
{
// Given N and K
int N = 5, K = 3;
String_palindrome(N, K);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement the above approach
# Function to generate a string of
# length N having longest palindromic
# substring of length K
def string_palindrome(N, K):
# Fill first K characters with 'a'
for i in range(K):
print("a", end = "")
# Stores a non-palindromic sequence
# to be repeated for N - k slots
s = "bcd"
# PrN - k remaining characters
for i in range(N - K):
print(s[i % 3], end = "")
# Driver Code
if __name__ == '__main__':
# Given N and K
N, K = 5, 3
string_palindrome(N, K)
# This code is contributed by mohit kumar 29
C#
// C# program to implement the above approach
using System;
class GFG
{
// Function to generate a String of
// length N having longest palindromic
// subString of length K
static void String_palindrome(int N, int K)
{
// Fill first K characters with 'a'
for (int i = 0; i < K; i++)
Console.Write("a");
// Stores a non-palindromic sequence
// to be repeated for N - k slots
string s = "bcd";
// Print N - k remaining characters
for (int i = 0; i < N - K; i++)
Console.Write(s[i % 3]);
}
// Driver Code
public static void Main(string[] args)
{
// Given N and K
int N = 5, K = 3;
String_palindrome(N, K);
}
}
// This code is contributed by AnkThon
Javascript
输出:
aaabc
时间复杂度: O(N)
辅助空间: O(1)