Rearrange characters in a string such that no two adjacent are same using hashing
Given a string str with repeated characters, the task is to rearrange the characters in a string such that no two adjacent characters are the same. If it is possible then print Yes else print No.
Examples:
Input: str = “geeksforgeeks”
Output: Yes
“egeksforegeks” is one such arrangement.
Input: str = “bbbbb”
Output: No
Approach: The idea is to store the frequency of each character in an unordered_map and compare maximum frequency of character with the difference of string length and maximum frequency number. If the maximum frequency is less than the difference then it can be arranged otherwise not.
- Let we start putting all the character having maximum frequency alternatively. Then at minimum, we need (max_freq-1) spaces between them to solve the question so that they are not adjacent to each other.
- But we have (length of the string – max_freq) spaces left. So, (length of the string – max_freq) should be at least (max_freq-1) to arrange such that no two char are the same.
- So, it goes this way: (max_freq-1) <= (length of the string – max_freq)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <time.h>
using namespace std;
int isPossible(string str)
{
unordered_map< char , int > freq;
int max_freq = 0;
for ( int j = 0; j < (str.length()); j++) {
freq[str[j]]++;
if (freq[str[j]] > max_freq)
max_freq = freq[str[j]];
}
if (max_freq <= (str.length() - max_freq + 1))
return true ;
return false ;
}
int main()
{
string str = "geeksforgeeks" ;
if (isPossible(str))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean isPossible( char [] str)
{
Map<Character, Integer> freq = new HashMap<>();
int max_freq = 0 ;
for ( int j = 0 ; j < (str.length); j++) {
if (freq.containsKey(str[j])) {
freq.put(str[j], freq.get(str[j]) + 1 );
if (freq.get(str[j]) > max_freq)
max_freq = freq.get(str[j]);
}
else {
freq.put(str[j], 1 );
if (freq.get(str[j]) > max_freq)
max_freq = freq.get(str[j]);
}
}
if (max_freq <= (str.length - max_freq + 1 ))
return true ;
return false ;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
if (isPossible(str.toCharArray()))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isPossible( Str ):
freq = dict ()
max_freq = 0
for j in range ( len ( Str )):
freq[ Str [j]] = freq.get( Str [j], 0 ) + 1
if (freq[ Str [j]] > max_freq):
max_freq = freq[ Str [j]]
if (max_freq < = ( len ( Str ) - max_freq + 1 )):
return True
return False
Str = "geeksforgeeks"
if (isPossible( Str )):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static Boolean isPossible( char [] str)
{
Dictionary< char , int > freq = new Dictionary< char , int >();
int max_freq = 0;
for ( int j = 0; j < (str.Length); j++) {
if (freq.ContainsKey(str[j])) {
var v = freq[str[j]] + 1;
freq.Remove(str[j]);
freq.Add(str[j], v);
if (freq[str[j]] > max_freq)
max_freq = freq[str[j]];
}
else {
freq.Add(str[j], 1);
if (freq[str[j]] > max_freq)
max_freq = freq[str[j]];
}
}
if (max_freq <= (str.Length - max_freq + 1))
return true ;
return false ;
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
if (isPossible(str.ToCharArray()))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPossible(str)
{
let freq = new Map();
let max_freq = 0;
for (let j = 0; j < (str.length); j++) {
if (freq.has(str[j])) {
freq.set(str[j], freq.get(str[j]) + 1);
if (freq.get(str[j]) > max_freq)
max_freq = freq.get(str[j]);
}
else {
freq.set(str[j], 1);
if (freq.get(str[j]) > max_freq)
max_freq = freq.get(str[j]);
}
}
if (max_freq <= (str.length - max_freq + 1))
return true ;
return false ;
}
let str = "geeksforgeeks" ;
if (isPossible(str.split( '' )))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are using frequency map.
Last Updated :
31 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...