给定一个字符串s组成的小写字符(AZ)只,任务是通过最大化回文子的数量这样的方式重新排列字符串打印一个新的字符串。如果有多个答案,请打印任何一个。
注意:即使某些子字符串重合,也要对它们进行计数,使其与所获得的字符串出现的次数相同。
例子:
Input: s = “aabab”
Output: ababa
string “ababa” has 9 palindromic substrings: “a”, “b”, “a”, “b”, “a”, “aba”, “bab”, “aba”, “ababa”.
Input: s = “aa”
Output: aa
The given string has the maximum number of palindromic substrings possible, “a”, “a” and “aa”.
该问题看似很复杂,但要解决各种情况,并进行观察将得出一个简单的解决方案。
一个简单的解决方案是对字符串进行排序并打印。排序需要O(N * log N) 。
一种有效的解决方案是使用freq []数组计算每个字符的频率,然后使用freq []数组构造字符串。
下面是上述方法的实现。
C++
// C++ program to rearrange the
// string such to maximize the
// number of palindromic substrings
#include
using namespace std;
// Function to return the newString
string newString(string s)
{
// length of string
int l = s.length();
// hashing array
int freq[26] = { 0 };
// iterate and count
for (int i = 0; i < l; i++) {
freq[s[i] - 'a'] += 1;
}
// resulting string
string ans = "";
// form the resulting string
for (int i = 0; i < 26; i++) {
// number of times character appears
for (int j = 0; j < freq[i]; j++) {
// append to resulting string
ans += (char)(97 + i);
}
}
return ans;
}
// Driver Code
int main()
{
string s = "aabab";
cout << newString(s);
return 0;
}
Java
// Java program to rearrange the
// string such to maximize the
// number of palindromic substrings
public class GFG {
// Function to return the newString
static String newString(String s)
{
// length of string
int l = s.length();
// hashing array
int freq[] = new int [26] ;
// iterate and count
for (int i = 0; i < l; i++) {
freq[s.charAt(i) - 'a'] += 1;
}
// resulting string
String ans = "";
// form the resulting string
for (int i = 0; i < 26; i++) {
// number of times character appears
for (int j = 0; j < freq[i]; j++) {
// append to resulting string
ans += (char)(97 + i);
}
}
return ans;
}
// Driver code
public static void main(String args[])
{
String s = "aabab";
System.out.println(newString(s));
}
// This Code is contributed by ANKITRAI1
}
Python3
# Python3 program to rearrange the
# string such to maximize the
# number of palindromic substrings
# Function to return the newString
def newString(s):
# length of string
l = len(s)
# hashing array
freq = [0] * (26)
# iterate and count
for i in range(0, l):
freq[ord(s[i]) - ord('a')] += 1
# resulting string
ans = ""
# form the resulting string
for i in range(0, 26):
# number of times character appears
for j in range(0, freq[i]):
# append to resulting string
ans += chr(97 + i)
return ans
# Driver Code
if __name__ == "__main__":
s = "aabab"
print(newString(s))
# This code is contributed by Rituraj Jain
C#
// C# program to rearrange the
// string such to maximize the
// number of palindromic substrings
using System;
class GFG
{
// Function to return the newString
static String newString(string s)
{
// length of string
int l = s.Length;
// hashing array
int[] freq = new int [26];
// iterate and count
for (int i = 0; i < l; i++)
{
freq[s[i] - 'a'] += 1;
}
// resulting string
string ans = "";
// form the resulting string
for (int i = 0; i < 26; i++)
{
// number of times character appears
for (int j = 0; j < freq[i]; j++)
{
// append to resulting string
ans += (char)(97 + i);
}
}
return ans;
}
// Driver code
public static void Main()
{
string s = "aabab";
Console.Write(newString(s));
}
}
// This code is contributed
// by ChitraNayal
Javascript
输出:
aaabb
时间复杂度:O(N)
辅助空间: O(26)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。