Given a string str with repeated characters, the task is to rearrange the characters in a string such that no two adjacent characters are 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.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> #include <time.h> using namespace std; // Function that returns true if it is possible // to rearrange the characters of the string // such that no two consecutive characters are same int isPossible(string str) { // To store the frequency of // each of the character unordered_map<char, int> freq; // To store the maximum frequency so far 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 possible if (max_freq <= (str.length() - max_freq + 1)) return true; return false; } // Driver code int main() { string str = "geeksforgeeks"; if (isPossible(str)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true if it is possible // to rearrange the characters of the string // such that no two consecutive characters are same static boolean isPossible(char[] str) { // To store the frequency of // each of the character Map<Character, Integer> freq = new HashMap<>(); // To store the maximum frequency so far 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 possible if (max_freq <= (str.length - max_freq + 1)) return true; return false; } // Driver code public static void main(String[] args) { String str = "geeksforgeeks"; if (isPossible(str.toCharArray())) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 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 def isPossible(Str): # To store the frequency of # each of the character freq = dict() # To store the maximum frequency so far 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 possible if (max_freq <= (len(Str) - max_freq + 1)): return True return False # Driver code Str = "geeksforgeeks" if (isPossible(Str)): print("Yes") else: print("No") # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Function that returns true if it is possible // to rearrange the characters of the string // such that no two consecutive characters are same static Boolean isPossible(char[] str) { // To store the frequency of // each of the character Dictionary<char, int> freq = new Dictionary<char, int>(); // To store the maximum frequency so far 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 possible if (max_freq <= (str.Length - max_freq + 1)) return true; return false; } // Driver code public static void Main(String[] args) { String str = "geeksforgeeks"; if (isPossible(str.ToCharArray())) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Princi Singh |
Yes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Rearrange characters in a string such that no two adjacent are same
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Rearrange string such that no pair of adjacent characters are of the same type
- Rearrange numbers in an array such that no two adjacent numbers are same
- Replace '?' in a string such that no two adjacent characters are same
- String with k distinct characters and no same characters adjacent
- Rearrange a string so that all same characters become atleast d distance away
- Rearrange a string so that all same characters become d distance away
- Minimum string such that every adjacent character of given string is still adjacent
- Rearrange the given string such that all prime multiple indexes have same character
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Rearrange array such that difference of adjacent elements is in descending order
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Check whether two strings contain same characters in same order
- Min flips of continuous characters to make all characters same in a string
- String hashing using Polynomial rolling hash function
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Minimum Cost of deletions such that string does not contains same consecutive characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

