text: Rearrange characters in a string such that no two adjacent are same using hashing | C ++ implementation of the approach ; Function that returns true if it is possible to rearrange the characters of the string such that no two consecutive characters are same ; To store the frequency of each of the character ; To store the maximum frequency so far ; If possible ; Driver code
code: #include <bits/stdc++.h> NEW_LINE #include <time.h> NEW_LINE 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 ; }