鉴于由小写字母的字符串str,任务是通过交换任何一对相邻的字符从字符串任意次数来构建字典序最小的非回文的字符串。
如果给定的字符串不能转换为词典上最小的非回文字符串,则打印“ -1” 。
例子:
Input: str = “djfbw”
Output: bdfjw
Explanation:
Perform the following swap operations to get the lexicographically smallest non-palindromic string:
Swap ‘b’ and ‘f’, str becomes “djbfw”
Swap ‘j’ and ‘b’, str becomes “dbjfw”
Swap ‘b’ and ‘d’, str becomes “bdjfw”
Swap ‘j’ and ‘f’, str becomes “bdfjw”.
Now “bdfjw” is the lexicographically smallest string which is not a palindrome.
Input: str[] = “pppppp”
Output: -1
天真的方法:这个想法是生成字符串的所有可能的排列,并检查它们是否形成回文。打印其中最小的排列。
时间复杂度: O(N * N!)
辅助空间: O(N)
有效的方法:我们的想法是要检查,如果字典序最小的字符串从给定的字符串可能是回文与否。步骤如下:
- 为了获得字典序最小的字符串,排序给定字符串中增加的顺序来安排字符串的字符。
- 现在,如果排序后的字符串是回文,则意味着该字符串仅具有一种字符类型,并且无法将其排列成不是回文的字符串。
- 如果不是回文,则按字典顺序排序的字符串是不是回文的最小字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the lexographically
// smallest string which is non-palindrome
void smallestNonPalindromic(string& s)
{
// Sort the given string
sort(s.begin(), s.end());
// Store reverse of sorted string
string reversestring
= string(s.rbegin(), s.rend());
// Check if the sorted string is
// palindromic or not
if (s != reversestring) {
cout << s;
}
else {
cout << "-1";
}
}
// Driver Code
int main()
{
// Given string str
string str = "asmfjdeovnhekfnj";
// Function Call
smallestNonPalindromic(str);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// reverse string
static String reverse(String input)
{
char[] a = input.toCharArray();
int l, r = a.length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
// Method to sort a string alphabetically
static String sortString(String inputString)
{
// convert input string to char array
char tempArray[] = inputString.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
// return new sorted string
return new String(tempArray);
}
// Function to find the lexographically
// smallest String which is non-palindrome
static void smallestNonPalindromic(String s)
{
// Sort the given String
s = sortString(s);
// Store reverse of sorted String
String reverseString = reverse(s);
// Check if the sorted String is
// palindromic or not
if (s != reverseString)
{
System.out.print(s);
}
else
{
System.out.print("-1");
}
}
// Driver Code
public static void main(String[] args)
{
// Given String str
String str = "asmfjdeovnhekfnj";
// Function Call
smallestNonPalindromic(str);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program for the above approach
# Function to find the lexographically
# smallest string which is non-palindrome
def smallestNonPalindromic(s):
# Sort the given string
s = sorted(s)
# Store reverse of sorted string
reversestring = s[::-1]
# Check if the sorted string is
# palindromic or not
if (s != reversestring):
print("".join(s))
else:
print("-1")
# Driver Code
if __name__ == '__main__':
# Given string str
str = "asmfjdeovnhekfnj"
# Function call
smallestNonPalindromic(str)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Reverse string
static String reverse(String input)
{
char[] a = input.ToCharArray();
int l, r = a.Length - 1;
for(l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join("", a);
}
// Method to sort a string alphabetically
static String sortString(String inputString)
{
// Convert input string to char array
char []tempArray = inputString.ToCharArray();
// Sort tempArray
Array.Sort(tempArray);
// Return new sorted string
return new String(tempArray);
}
// Function to find the lexographically
// smallest String which is non-palindrome
static void smallestNonPalindromic(String s)
{
// Sort the given String
s = sortString(s);
// Store reverse of sorted String
String reverseString = reverse(s);
// Check if the sorted String is
// palindromic or not
if (s != reverseString)
{
Console.Write(s);
}
else
{
Console.Write("-1");
}
}
// Driver Code
public static void Main(String[] args)
{
// Given String str
String str = "asmfjdeovnhekfnj";
// Function call
smallestNonPalindromic(str);
}
}
// This code is contributed by Amit Katiyar
adeeffhjjkmnnosv
时间复杂度: O(N)
辅助空间: O(1)