给定一个包含小写字母(a – z)的字符串str 。任务是在重新排列某些字符后打印该字符串,以使该字符串变为非回文。如果不可能使字符串非回文,则打印-1 。
例子:
Input: str = “abba”
Output: aabb
Input: str = “zzz”
Output: -1
方法:如果字符串中的所有字符都相同,那么无论您如何重新排列字符,字符串都将保持不变并且回文。现在,如果存在非回文排列,则重新排列字符的最佳方法是对字符串进行排序,该字符串将形成相同字符的连续段,并且永远不会回文。为了减少对字符串进行排序所需的时间,我们可以存储所有26个字符的频率并以排序方式打印它们。
下面是上述方法的实现:
C++
// CPP Program to rearrange letters of string
// to find a non-palindromic string if it exists
#include
using namespace std;
// Function to print the non-palindromic string
// if it exists, otherwise prints -1
void findNonPalinString(string s)
{
int freq[26] = { 0 }, flag = 0;
for (int i = 0; i < s.size(); i++) {
// If all characters are not
// same, set flag to 1
if (s[i] != s[0])
flag = 1;
// Update frequency of the current character
freq[s[i] - 'a']++;
}
// If all characters are same
if (!flag)
cout << "-1";
else {
// Print characters in sorted manner
for (int i = 0; i < 26; i++)
for (int j = 0; j < freq[i]; j++)
cout << char('a' + i);
}
}
// Driver Code
int main()
{
string s = "abba";
findNonPalinString(s);
return 0;
}
Java
// Java Program to rearrange letters of string
// to find a non-palindromic string if it exists
class GfG
{
// Function to print the non-palindromic string
// if it exists, otherwise prints -1
static void findNonPalinString(char s[])
{
int freq[] = new int[26];
int flag = 0;
for (int i = 0; i < s.length; i++)
{
// If all characters are not
// same, set flag to 1
if (s[i] != s[0])
flag = 1;
// Update frequency of
// the current character
freq[s[i] - 'a']++;
}
// If all characters are same
if (flag == 0)
System.out.println("-1");
else
{
// Print characters in sorted manner
for (int i = 0; i < 26; i++)
for (int j = 0; j < freq[i]; j++)
System.out.print((char)('a' + i));
}
}
// Driver Code
public static void main(String[] args)
{
String s = "abba";
findNonPalinString(s.toCharArray());
}
}
// This code is contributed by
// Prerna Saini.
Python3
# Python3 Program to rearrange letters of string
# to find a non-palindromic string if it exists
# Function to print the non-palindromic string
# if it exists, otherwise prints -1
def findNonPalinString(s):
freq = [0] * (26)
flag = 0
for i in range(0, len(s)):
# If all characters are not same,
# set flag to 1
if s[i] != s[0]:
flag = 1
# Update frequency of the current
# character
freq[ord(s[i]) - ord('a')] += 1
# If all characters are same
if not flag:
print("-1")
else:
# Print characters in sorted manner
for i in range(0, 26):
for j in range(0, freq[i]):
print(chr(ord('a') + i),
end = "")
# Driver Code
if __name__ == "__main__":
s = "abba"
findNonPalinString(s)
# This code is contributed by
# Rituraj Jain
C#
// C# Program to rearrange letters
// of string to find a non-palindromic
// string if it exists
using System;
class GfG
{
// Function to print the
// non-palindromic string
// if it exists, otherwise
// prints -1
static void findNonPalinString(char []s)
{
int []freq = new int[26];
int flag = 0;
for (int i = 0; i < s.Length; i++)
{
// If all characters are not
// same, set flag to 1
if (s[i] != s[0])
flag = 1;
// Update frequency of
// the current character
freq[s[i] - 'a']++;
}
// If all characters are same
if (flag == 0)
Console.WriteLine("-1");
else
{
// Print characters in sorted manner
for (int i = 0; i < 26; i++)
for (int j = 0; j < freq[i]; j++)
Console.Write((char)('a' + i));
}
}
// Driver Code
public static void Main()
{
string s = "abba";
findNonPalinString(s.ToCharArray());
}
}
// This code is contributed by Ryuga
输出:
aabb