Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
Last Updated :
07 Mar, 2022
Given string str consisting of lowercase alphabets, the task is to construct the lexicographically smallest non-palindromic string by swapping any pair of adjacent characters from the string any number of times.
If the given string cannot be converted to a lexicographically smallest non-palindromic string, then print “-1”.
Examples:
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
Naive Approach: The idea is to generate all possible permutations of the string and to check if they form palindrome or not. Print the smallest permutation among them.
Time Complexity: O(N*N!)
Auxiliary Space: O(N)
Efficient Approach: The idea is to check if lexicographically the smallest string possible from the given string is a palindrome or not. Below are the steps:
- To obtain lexicographically the smallest string, sort the given string to arrange the characters of the string in increasing order.
- Now, if the sorted string is a palindrome, then it means that the string has only one type of character and it can not be arranged to form a string which is not a palindrome.
- If it is not a palindrome, then the sorted string is lexicographically the smallest string which is not a palindrome.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void smallestNonPalindromic(string& s)
{
sort(s.begin(), s.end());
string reversestring
= string(s.rbegin(), s.rend());
if (s != reversestring) {
cout << s;
}
else {
cout << "-1" ;
}
}
int main()
{
string str = "asmfjdeovnhekfnj" ;
smallestNonPalindromic(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
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);
}
static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static void smallestNonPalindromic(String s)
{
s = sortString(s);
String reverseString = reverse(s);
if (s != reverseString)
{
System.out.print(s);
}
else
{
System.out.print( "-1" );
}
}
public static void main(String[] args)
{
String str = "asmfjdeovnhekfnj" ;
smallestNonPalindromic(str);
}
}
|
Python3
def smallestNonPalindromic(s):
s = sorted (s)
reversestring = s[:: - 1 ]
if (s ! = reversestring):
print ("".join(s))
else :
print ( "-1" )
if __name__ = = '__main__' :
str = "asmfjdeovnhekfnj"
smallestNonPalindromic( str )
|
C#
using System;
class GFG{
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);
}
static String sortString(String inputString)
{
char []tempArray = inputString.ToCharArray();
Array.Sort(tempArray);
return new String(tempArray);
}
static void smallestNonPalindromic(String s)
{
s = sortString(s);
String reverseString = reverse(s);
if (s != reverseString)
{
Console.Write(s);
}
else
{
Console.Write( "-1" );
}
}
public static void Main(String[] args)
{
String str = "asmfjdeovnhekfnj" ;
smallestNonPalindromic(str);
}
}
|
Javascript
<script>
function smallestNonPalindromic(s) {
var newStr = s.split( "" );
newStr.sort().reverse();
var reversestring = newStr.reverse().join( "" );
if (s !== reversestring) {
document.write(newStr.join( "" ));
} else {
document.write( "-1" );
}
}
var str = "asmfjdeovnhekfnj" ;
smallestNonPalindromic(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...