Rearrange string such that no pair of adjacent characters are of the same type
Last Updated :
06 Feb, 2023
Given alphanumeric string str, the task is to rearrange the string such that no two adjacent characters are of the same type, i.e., no two adjacent characters can be alphabets or digits. If no such arrangement is possible, print -1.
Examples:
Input: str = “geeks2020”
Output: g2e0e2k0s
Input: str = “IPL20”
Output: I2P0L
Naive Approach: The simplest approach is to generate all possible permutation of the given string and for every permutation, check if it satisfies the given conditions or not. If found to be true for any permutation, print that permutation. If no such permutation exists, then print -1.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to store all the alphabets and the digits separately and rearrange them by placing them alternatively in the resultant string. If the count of the alphabets and the digits differ by more than 1, print -1 as no desired arrangement is possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string rearrange(string s)
{
string s1 = "" , s2 = "" ;
for ( char x : s) {
isalpha (x) ? s1.push_back(x)
: s2.push_back(x);
}
int n = s1.size();
int m = s2.size();
if ( abs (n - m) > 1)
return "-1" ;
int i = 0, j = 0, k = 0;
int flag = (n >= m) ? 1 : 0;
while (i < n and j < m) {
if (flag)
s[k++] = s1[i++];
else
s[k++] = s2[j++];
flag = !flag;
}
return s;
}
int main()
{
string str = "geeks2020" ;
cout << rearrange(str) << endl;
return 0;
}
|
Java
class GFG{
static String rearrange(String s)
{
String s1 = "" , s2 = "" , ans = "" ;
char []s3 = s.toCharArray();
for ( char x : s3)
{
if (x >= 'a' && x <= 'z' )
s1 += x ;
else
s2 += x;
}
int n = s1.length();
int m = s2.length();
if (Math.abs(n - m) > 1 )
return "-1" ;
int i = 0 , j = 0 , k = 0 ;
int flag = (n >= m) ? 1 : 0 ;
while (i < n && j < m)
{
if (flag != 0 )
ans += s1.charAt(i++);
else
ans += s2.charAt(j++);
if (flag == 1 )
flag = 0 ;
else
flag = 1 ;
}
return ans;
}
public static void main(String[] args)
{
String str = "geeks2020" ;
System.out.print(rearrange(str) + "\n" );
}
}
|
Python3
def rearrange(s):
s1 = []
s2 = []
for x in s:
if x.isalpha():
s1.append(x)
else :
s2.append(x)
n = len (s1)
m = len (s2)
if ( abs (n - m) > 1 ):
return "-1"
i = 0
j = 0
k = 0
flag = 0
if (n > = m):
flag = 1
else :
flag = 0
while (i < n and j < m):
if (flag):
s[k] = s1[i]
k + = 1
i + = 1
else :
s[k] = s2[j]
k + = 1
j + = 1
flag = not flag
return "".join(s)
if __name__ = = '__main__' :
str = "geeks2020"
str1 = [i for i in str ]
print (rearrange(str1))
|
C#
using System;
class GFG{
static String rearrange(String s)
{
String s1 = "" , s2 = "" , ans = "" ;
char []s3 = s.ToCharArray();
foreach ( char x in s3)
{
if (x >= 'a' && x <= 'z' )
s1 += x ;
else
s2 += x;
}
int n = s1.Length;
int m = s2.Length;
if (Math.Abs(n - m) > 1)
return "-1" ;
int i = 0, j = 0, k = 0;
int flag = (n >= m) ? 1 : 0;
while (i < n && j < m)
{
if (flag != 0)
ans += s1[i++];
else
ans += s2[j++];
if (flag == 1)
flag = 0;
else
flag = 1;
}
return ans;
}
public static void Main(String[] args)
{
String str = "geeks2020" ;
Console.Write(rearrange(str) + "\n" );
}
}
|
Javascript
<script>
function rearrange(s)
{
let s1 = "" , s2 = "" , ans = "" ;
let s3 = s.split( "" );
for (let x = 0; x < s3.length; x++)
{
if (s3[x] >= 'a' && s3[x] <= 'z' )
s1 += s3[x] ;
else
s2 += s3[x];
}
let n = s1.length;
let m = s2.length;
if (Math.abs(n - m) > 1)
return "-1" ;
let i = 0, j = 0, k = 0;
let flag = (n >= m) ? 1 : 0;
while (i < n && j < m)
{
if (flag != 0)
ans += s1[i++];
else
ans += s2[j++];
if (flag == 1)
flag = 0;
else
flag = 1;
}
return ans;
}
let str = "geeks2020" ;
document.write(rearrange(str) + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...