给定一个字符串S选自N小写字母,该任务是从其频率不是2的幂,并且然后进行排序以升序字符串的字符串中删除的字符。
例子:
Input: S = “aaacbb”
Output: bbc
Explanation: The frequencies of ‘a’, ‘b’, and ‘c’ in the given string are 3, 2, 1. Only the character ‘a’ has frequency (= 3) which is not a power of 2. Therefore, removing ‘a’ from the string S modifies it to “cbb”. Therefore, the modified string after sorting is “bbc”.
Input: S = “geeksforgeeks”
Output: eeeefggkkorss
方法:给定的问题可以通过使用哈希来解决。请按照以下步骤解决给定的问题:
- 初始化一个大小为26的辅助数组,比如freq[] ,以存储字符串S中每个字符的频率,使得索引0表示字符‘a’ , 1表示字符‘b’ ,依此类推。
- 遍历给定的字符串S并将数组freq[]中每个字符S[i]的频率增加1 。
- 遍历数组freq[] ,如果freq[i]的值是 2 的幂,则打印字符(‘a’ + i), freq[i]次。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to remove all the characters
// from a string that whose frequencies
// are not equal to a perfect power of 2
void countFrequency(string S, int N)
{
// Stores the frequency of
// each character in S
int freq[26] = { 0 };
// Iterate over characters of string
for (int i = 0; i < N; i++) {
// Update frequency of current
// character in the array freq[]
freq[S[i] - 'a']++;
}
// Traverse the array freq[]
for (int i = 0; i < 26; i++) {
// Check if the i-th letter
// is absent from string S
if (freq[i] == 0)
continue;
// Calculate log of frequency
// of the current character
// in the string S
int lg = log2(freq[i]);
// Calculate power of 2 of lg
int a = pow(2, lg);
// Check if freq[i]
// is a power of 2
if (a == freq[i]) {
// Print letter i + 'a'
// freq[i] times
while (freq[i]--)
cout << (char)(i + 'a');
}
}
}
// Driver Code
int main()
{
string S = "aaacbb";
int N = S.size();
countFrequency(S, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to remove all the characters
// from a string that whose frequencies
// are not equal to a perfect power of 2
static void countFrequency(String S, int N)
{
// Stores the frequency of
// each character in S
int []freq = new int[26];
//Array.Clear(freq, 0, freq.Length);
// Iterate over characters of string
for(int i = 0; i < N; i++)
{
// Update frequency of current
// character in the array freq[]
freq[(int)S.charAt(i) - 'a'] += 1;
}
// Traverse the array freq[]
for(int i = 0; i < 26; i++)
{
// Check if the i-th letter
// is absent from string S
if (freq[i] == 0)
continue;
// Calculate log of frequency
// of the current character
// in the string S
int lg = (int)(Math.log(freq[i]) / Math.log(2));
// Calculate power of 2 of lg
int a = (int)Math.pow(2, lg);
// Check if freq[i]
// is a power of 2
if (a == freq[i])
{
// Print letter i + 'a'
// freq[i] times
while (freq[i] > 0)
{
freq[i] -= 1;
System.out.print((char)(i + 'a'));
}
}
}
}
// Driver Code
public static void main(String args[])
{
String S = "aaacbb";
int N = S.length();
countFrequency(S, N);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
from math import log2
# Function to remove all the characters
# from a that whose frequencies are not
# equal to a perfect power of 2
def countFrequency(S, N):
# Stores the frequency of
# each character in S
freq = [0] * 26
# Iterate over characters of string
for i in range(N):
# Update frequency of current
# character in the array freq[]
freq[ord(S[i]) - ord('a')] += 1
# Traverse the array freq[]
for i in range(26):
# Check if the i-th letter
# is absent from S
if (freq[i] == 0):
continue
# Calculate log of frequency
# of the current character
# in the S
lg = int(log2(freq[i]))
# Calculate power of 2 of lg
a = pow(2, lg)
# Check if freq[i]
# is a power of 2
if (a == freq[i]):
# Print letter i + 'a'
# freq[i] times
while (freq[i]):
print(chr(i + ord('a')), end = "")
freq[i] -= 1
# Driver Code
if __name__ == '__main__':
S = "aaacbb"
N = len(S)
countFrequency(S, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to remove all the characters
// from a string that whose frequencies
// are not equal to a perfect power of 2
static void countFrequency(string S, int N)
{
// Stores the frequency of
// each character in S
int []freq = new int[26];
Array.Clear(freq, 0, freq.Length);
// Iterate over characters of string
for(int i = 0; i < N; i++)
{
// Update frequency of current
// character in the array freq[]
freq[(int)S[i] - 'a'] += 1;
}
// Traverse the array freq[]
for(int i = 0; i < 26; i++)
{
// Check if the i-th letter
// is absent from string S
if (freq[i] == 0)
continue;
// Calculate log of frequency
// of the current character
// in the string S
int lg = (int)Math.Log((double)freq[i], 2.0);
// Calculate power of 2 of lg
int a = (int)Math.Pow(2, lg);
// Check if freq[i]
// is a power of 2
if (a == freq[i])
{
// Print letter i + 'a'
// freq[i] times
while (freq[i] > 0)
{
freq[i] -= 1;
Console.Write((char)(i + 'a'));
}
}
}
}
// Driver Code
public static void Main()
{
string S = "aaacbb";
int N = S.Length;
countFrequency(S, N);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
输出:
bbc
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。