给定一个包含低位字母字符的字符串,我们最多需要从该字符串中删除一个字符,以使每个不同字符在字符串中的频率变得相同。
例子:
Input: str = “xyyz”
Output: Yes
We can remove character ’y’ from above
string to make the frequency of each
character same.
Input: str = “xyyzz”
Output: Yes
We can remove character ‘x’ from above
string to make the frequency of each
character same.
Input: str = “xxxxyyzz”
Output: No
It is not possible to make frequency of
each character same just by removing at
most one character from above string.
方法:这个问题可以使用散列的概念来解决。在这个问题中要注意的主要是字符的位置在这里无关紧要,所以我们将计算字符的频率,如果它们都相同,那么我们就完成了,不需要删除任何字符来计算频率。字符相同 否则我们可以一个一个地迭代所有字符并将它们的频率减一,如果所有频率都相同,那么我们将标记最多可以通过删除一个字符使字符频率相同,如果频率不匹配然后我们将再次增加该频率并循环其他字符。
以下是上述方法的试运行:
下面是上述方法的实现:
C++
// C++ program to get same frequency character
// string by removal of at most one char
#include
using namespace std;
#define M 26
// Utility method to get index of character ch
// in lower alphabet characters
int getIdx(char ch)
{
return (ch - 'a');
}
// Returns true if all non-zero elements
// values are same
bool allSame(int freq[], int N)
{
int same;
// get first non-zero element
int i;
for (i = 0; i < N; i++) {
if (freq[i] > 0) {
same = freq[i];
break;
}
}
// check equality of each element with variable same
for (int j = i + 1; j < N; j++)
if (freq[j] > 0 && freq[j] != same)
return false;
return true;
}
// Returns true if we can make all character
// frequencies same
bool possibleSameCharFreqByOneRemoval(string str)
{
int l = str.length();
// fill frequency array
int freq[M] = { 0 };
for (int i = 0; i < l; i++)
freq[getIdx(str[i])]++;
// if all frequencies are same, then return true
if (allSame(freq, M))
return true;
/* Try decreasing frequency of all character
by one and then check all equality of all
non-zero frequencies */
for (char c = 'a'; c <= 'z'; c++) {
int i = getIdx(c);
// Check character only if it occurs in str
if (freq[i] > 0) {
freq[i]--;
if (allSame(freq, M))
return true;
freq[i]++;
}
}
return false;
}
// Driver code to test above methods
int main()
{
string str = "xyyzz";
if (possibleSameCharFreqByOneRemoval(str))
cout << "Yes";
else
cout << "No";
}
Java
// Java program to get same frequency character
// string by removal of at most one char
public class GFG {
static final int M = 26;
// Utility method to get index of character ch
// in lower alphabet characters
static int getIdx(char ch)
{
return (ch - 'a');
}
// Returns true if all non-zero elements
// values are same
static boolean allSame(int freq[], int N)
{
int same = 0;
// get first non-zero element
int i;
for (i = 0; i < N; i++) {
if (freq[i] > 0) {
same = freq[i];
break;
}
}
// check equality of each element with
// variable same
for (int j = i + 1; j < N; j++)
if (freq[j] > 0 && freq[j] != same)
return false;
return true;
}
// Returns true if we can make all character
// frequencies same
static boolean possibleSameCharFreqByOneRemoval(String str)
{
int l = str.length();
// fill frequency array
int[] freq = new int[M];
for (int i = 0; i < l; i++)
freq[getIdx(str.charAt(i))]++;
// if all frequencies are same, then return true
if (allSame(freq, M))
return true;
/* Try decreasing frequency of all character
by one and then check all equality of all
non-zero frequencies */
for (char c = 'a'; c <= 'z'; c++) {
int i = getIdx(c);
// Check character only if it occurs in str
if (freq[i] > 0) {
freq[i]--;
if (allSame(freq, M))
return true;
freq[i]++;
}
}
return false;
}
// Driver code to test above methods
public static void main(String args[])
{
String str = "xyyzz";
if (possibleSameCharFreqByOneRemoval(str))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to get same frequency character
# string by removal of at most one char
M = 26
# Utility method to get index of character ch
# in lower alphabet characters
def getIdx(ch):
return (ord(ch) - ord('a'))
# Returns true if all non-zero elements
# values are same
def allSame(freq, N):
# get first non-zero element
for i in range(0, N):
if(freq[i] > 0):
same = freq[i]
break
# check equality of each element
# with variable same
for j in range(i + 1, N):
if(freq[j] > 0 and freq[j] != same):
return False
return True
# Returns true if we can make all
# character frequencies same
def possibleSameCharFreqByOneRemoval(str1):
l = len(str1)
# fill frequency array
freq = [0] * M
for i in range(0, l):
freq[getIdx(str1[i])] += 1
# if all frequencies are same,
# then return true
if(allSame(freq, M)):
return True
# Try decreasing frequency of all character
# by one and then check all equality of all
# non-zero frequencies
for i in range(0, 26):
# Check character only if it
# occurs in str
if(freq[i] > 0):
freq[i] -= 1
if(allSame(freq, M)):
return True
freq[i] += 1
return False
# Driver code
if __name__ == "__main__":
str1 = "xyyzz"
if(possibleSameCharFreqByOneRemoval(str1)):
print("Yes")
else:
print("No")
# This code is contributed by Sairahul099
C#
// C# program to get same frequency
// character string by removal of
// at most one char
using System;
class GFG {
static int M = 26;
// Utility method to get
// index of character ch
// in lower alphabet characters
static int getIdx(char ch)
{
return (ch - 'a');
}
// Returns true if all
// non-zero elements
// values are same
static bool allSame(int[] freq,
int N)
{
int same = 0;
// get first non-zero element
int i;
for (i = 0; i < N; i++) {
if (freq[i] > 0) {
same = freq[i];
break;
}
}
// check equality of
// each element with
// variable same
for (int j = i + 1; j < N; j++)
if (freq[j] > 0 && freq[j] != same)
return false;
return true;
}
// Returns true if we
// can make all character
// frequencies same
static bool possibleSameCharFreqByOneRemoval(string str)
{
int l = str.Length;
// fill frequency array
int[] freq = new int[M];
for (int i = 0; i < l; i++)
freq[getIdx(str[i])]++;
// if all frequencies are same,
// then return true
if (allSame(freq, M))
return true;
/* Try decreasing frequency of all
character by one and then check
all equality of all non-zero
frequencies */
for (char c = 'a'; c <= 'z'; c++) {
int i = getIdx(c);
// Check character only if
// it occurs in str
if (freq[i] > 0) {
freq[i]--;
if (allSame(freq, M))
return true;
freq[i]++;
}
}
return false;
}
// Driver code
public static void Main()
{
string str = "xyyzz";
if (possibleSameCharFreqByOneRemoval(str))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed
// by ChitraNayal
PHP
0)
{
$same = $freq[$i];
break;
}
}
// check equality of each
// element with variable same
for ($j = $i + 1; $j < $N; $j++)
if ($freq[$j] > 0 &&
$freq[$j] != $same)
return false;
return true;
}
// Returns true if we
// can make all character
// frequencies same
function possibleSameCharFreqByOneRemoval($str)
{
global $M;
$l = strlen($str);
// fill frequency array
$freq = array_fill(0, $M, NULL);
for ($i = 0; $i < $l; $i++)
$freq[getIdx($str[$i])]++;
// if all frequencies are same,
// then return true
if (allSame($freq, $M))
return true;
/* Try decreasing frequency of all
character by one and then check
all equality of all non-zero
frequencies */
for ($c = 'a'; $c <= 'z'; $c++)
{
$i = getIdx($c);
// Check character only
// if it occurs in str
if ($freq[$i] > 0)
{
$freq[$i]--;
if (allSame($freq, $M))
return true;
$freq[$i]++;
}
}
return false;
}
// Driver code
$str = "xyyzz";
if (possibleSameCharFreqByOneRemoval($str))
echo "Yes";
else
echo "No";
// This code is contributed
// by ChitraNayal
?>
Javascript
输出:
Yes
时间复杂度: O(n) 假设字母大小是恒定的。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。