Given string str of size N consists of lower-case English alphabets. The task is to find the arrangement of the characters of the string such that no two adjacent characters are neighbors in English alphabets. In case of multiple answers print any of them. If no such arrangement is possible then print -1.
Examples:
Input: str = “aabcd”
Output: bdaac
No two adjacent characters are neighbours in English alphabets.
Input: str = “aab”
Output: -1
Approach: Traverse through the string and store all odd positioned characters in a string odd and even positioned characters in another string even i.e. every two consecutive characters in both the strings will have an absolute difference in ASCII values of at least 2. Then sort both the strings. Now, if any of the concatenation (even + odd) or (odd + even) is valid then print the valid arrangement else it is not possible to rearrange the string in the required way.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check(string s)
{
for ( int i = 0; i + 1 < s.size(); ++i)
if ( abs (s[i] - s[i + 1]) == 1)
return false ;
return true ;
}
void Rearrange(string str)
{
string odd = "" , even = "" ;
for ( int i = 0; i < str.size(); ++i) {
if (str[i] % 2 == 0)
even += str[i];
else
odd += str[i];
}
sort(odd.begin(), odd.end());
sort(even.begin(), even.end());
if (check(odd + even))
cout << odd + even;
else if (check(even + odd))
cout << even + odd;
else
cout << -1;
}
int main()
{
string str = "aabcd" ;
Rearrange(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean check(String s)
{
for ( int i = 0 ; i + 1 < s.length(); ++i)
{
if (Math.abs(s.charAt(i) -
s.charAt(i + 1 )) == 1 )
{
return false ;
}
}
return true ;
}
static void Rearrange(String str)
{
String odd = "" , even = "" ;
for ( int i = 0 ; i < str.length(); ++i)
{
if (str.charAt(i) % 2 == 0 )
{
even += str.charAt(i);
}
else
{
odd += str.charAt(i);
}
}
odd = sort(odd);
even = sort(even);
if (check(odd + even))
{
System.out.print(odd + even);
}
else if (check(even + odd))
{
System.out.print(even + odd);
}
else
{
System.out.print(- 1 );
}
}
public static String sort(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static void main(String[] args)
{
String str = "aabcd" ;
Rearrange(str);
}
}
|
Python3
def check(s):
for i in range ( len (s) - 1 ):
if ( abs ( ord (s[i]) -
ord (s[i + 1 ])) = = 1 ):
return False
return True
def Rearrange( Str ):
odd, even = " "," "
for i in range ( len ( Str )):
if ( ord ( Str [i]) % 2 = = 0 ):
even + = Str [i]
else :
odd + = Str [i]
odd = sorted (odd)
even = sorted (even)
if (check(odd + even)):
print ("".join(odd + even))
elif (check(even + odd)):
print ("".join(even + odd))
else :
print ( - 1 )
Str = "aabcd"
Rearrange( Str )
|
C#
using System;
class GFG
{
static Boolean check(String s)
{
for ( int i = 0; i + 1 < s.Length; ++i)
{
if (Math.Abs(s[i] -
s[i + 1]) == 1)
{
return false ;
}
}
return true ;
}
static void Rearrange(String str)
{
String odd = "" , even = "" ;
for ( int i = 0; i < str.Length; ++i)
{
if (str[i] % 2 == 0)
{
even += str[i];
}
else
{
odd += str[i];
}
}
odd = sort(odd);
even = sort(even);
if (check(odd + even))
{
Console.Write(odd + even);
}
else if (check(even + odd))
{
Console.Write(even + odd);
}
else
{
Console.Write(-1);
}
}
public static String sort(String inputString)
{
char []tempArray = inputString.ToCharArray();
Array.Sort(tempArray);
return new String(tempArray);
}
public static void Main(String[] args)
{
String str = "aabcd" ;
Rearrange(str);
}
}
|
Javascript
<script>
function check(s) {
for ( var i = 0; i + 1 < s.length; ++i)
{
if (Math.abs(s[i].charCodeAt(0) -
s[i + 1].charCodeAt(0)) === 1)
{
return false ;
}
}
return true ;
}
function Rearrange(str) {
var odd = "" ,
even = "" ;
for ( var i = 0; i < str.length; ++i) {
if (str[i].charCodeAt(0) % 2 === 0) {
even += str[i];
} else {
odd += str[i];
}
}
odd.split( "" ).sort((a, b) => a - b);
even.split( "" ).sort((a, b) => a - b);
if (check(odd + even)) {
document.write(odd + even);
} else if (check(even + odd)) {
document.write(even + odd);
} else {
document.write(-1);
}
}
var str = "aabcd" ;
Rearrange(str);
</script>
|
Time Complexity: Onlogn)
Auxiliary Space: O(n)