给定具有重复字符的字符串str ,任务是重新排列字符串的字符,以使两个相邻字符都不相同。如果可能,则打印“是”,否则打印“否” 。
例子:
Input: str = “geeksforgeeks”
Output: Yes
“egeksforegeks” is one such arrangement.
Input: str = “bbbbb”
Output: No
方法:想法是将每个字符的频率存储在unordered_map中,并将字符的最大频率与字符串长度和最大频率数之差进行比较。如果最大频率小于该差,则可以否则安排它。
- 让我们开始交替放置具有最大频率的所有字符。然后,我们至少需要在它们之间使用(max_freq-1)个空格来解决问题,以使它们彼此不相邻。
- 但是,我们还剩下(字符串的长度– max_freq)空格。因此,(字符串的长度– max_freq)应至少为(max_freq-1),以使两个字符都不相同。
- 因此,它是这样的:(max_freq + 1)<=(字符串的长度– max_freq)
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#include
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 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 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 freq = new Dictionary();
// 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