从字符串中删除严格少于 K 次的字符
给定字符串小写字母和一个数字 K。任务是通过删除字符中出现严格少于 K 次的字符串来减少它。
例子:
Input : str = "geeksforgeeks", K = 2
Output : geeksgeeks
Input : str = "geeksforgeeks", K = 3
Output : eeee
方法 :
- 创建一个包含 26 个索引的哈希表,其中第 0 个索引表示“a”,第 1 个索引表示“b”,依此类推,以存储输入字符串中每个字符的频率。将此哈希表初始化为零。
- 遍历字符串并增加哈希表中每个字符的频率。即哈希[str[i]-'a']++。
- 现在创建一个新的空字符串并再次遍历输入字符串并仅附加新字符串中哈希表中频率大于或等于 k 的那些字符,并跳过那些出现少于 k 次的字符。
下面是上述方法的实现:
C++
// C++ program to reduce the string by
// removing the characters which
// appears less than k times
#include
using namespace std;
const int MAX_CHAR = 26;
// Function to reduce the string by
// removing the characters which
// appears less than k times
string removeChars(string str, int k)
{
// Hash table initialised to 0
int hash[MAX_CHAR] = { 0 };
// Increment the frequency of the character
int n = str.length();
for (int i = 0; i < n; ++i)
hash[str[i] - 'a']++;
// create a new empty string
string res = "";
for (int i = 0; i < n; ++i) {
// Append the characters which
// appears more than equal to k times
if (hash[str[i] - 'a'] >= k) {
res += str[i];
}
}
return res;
}
// Driver Code
int main()
{
string str = "geeksforgeeks";
int k = 2;
cout << removeChars(str, k);
return 0;
}
Java
// Java program to reduce the string by
// removing the characters which
// appears less than k times
class GFG {
final static int MAX_CHAR = 26;
// Function to reduce the string by
// removing the characters which
// appears less than k times
static String removeChars(String str, int k) {
// Hash table initialised to 0
int hash[] = new int[MAX_CHAR];
// Increment the frequency of the character
int n = str.length();
for (int i = 0; i < n; ++i) {
hash[str.charAt(i) - 'a']++;
}
// create a new empty string
String res = "";
for (int i = 0; i < n; ++i) {
// Append the characters which
// appears more than equal to k times
if (hash[str.charAt(i) - 'a'] >= k) {
res += str.charAt(i);
}
}
return res;
}
// Driver Code
static public void main(String[] args) {
String str = "geeksforgeeks";
int k = 2;
System.out.println(removeChars(str, k));
}
}
// This code is contributed by 29AjayKumar
Python 3
# Python 3 program to reduce the string
# by removing the characters which
# appears less than k times
MAX_CHAR = 26
# Function to reduce the string by
# removing the characters which
# appears less than k times
def removeChars(str, k):
# Hash table initialised to 0
hash = [0] * (MAX_CHAR)
# Increment the frequency of
# the character
n = len(str)
for i in range(n):
hash[ord(str[i]) - ord('a')] += 1
# create a new empty string
res = ""
for i in range(n):
# Append the characters which
# appears more than equal to k times
if (hash[ord(str[i]) - ord('a')] >= k) :
res += str[i]
return res
# Driver Code
if __name__ == "__main__":
str = "geeksforgeeks"
k = 2
print(removeChars(str, k))
# This code is contributed by ita_c
C#
// C# program to reduce the string by
// removing the characters which
// appears less than k times
using System;
class GFG
{
readonly static int MAX_CHAR = 26;
// Function to reduce the string by
// removing the characters which
// appears less than k times
static String removeChars(String str, int k)
{
// Hash table initialised to 0
int []hash = new int[MAX_CHAR];
// Increment the frequency of the character
int n = str.Length;
for (int i = 0; i < n; ++i)
{
hash[str[i] - 'a']++;
}
// create a new empty string
String res = "";
for (int i = 0; i < n; ++i)
{
// Append the characters which
// appears more than equal to k times
if (hash[str[i] - 'a'] >= k)
{
res += str[i];
}
}
return res;
}
// Driver Code
static public void Main()
{
String str = "geeksforgeeks";
int k = 2;
Console.WriteLine(removeChars(str, k));
}
}
// This code is contributed by Rajput-Ji
Javascript
Python3
# Python 3 program to reduce the string
# by removing the characters which
# appears less than k times
from collections import Counter
# Function to reduce the string by
# removing the characters which
# appears less than k times
def removeChars(str, k):
# Using Counter function to
# count frequencies
freq = Counter(str)
# Create a new empty string
res = ""
for i in range(len(str)):
# Append the characters which
# appears more than equal to k times
if (freq[str[i]] >= k):
res += str[i]
return res
# Driver Code
if __name__ == "__main__":
str = "geeksforgeeks"
k = 2
print(removeChars(str, k))
# This code is contributed by vikkycirus
输出:
geeksgeeks
时间复杂度:O(N),其中 N 是给定字符串的长度。
方法#2:使用内置Python函数:
- 我们将使用内置的Counter()函数扫描字符串并计算所有字符的出现次数。
- 现在创建一个新的空字符串,再次遍历输入字符串,只在新字符串中追加频率字典值大于或等于 k 的字符,跳过出现次数少于 k 次的字符。
注意:此方法适用于所有类型的字符。
下面是上述方法的实现:
Python3
# Python 3 program to reduce the string
# by removing the characters which
# appears less than k times
from collections import Counter
# Function to reduce the string by
# removing the characters which
# appears less than k times
def removeChars(str, k):
# Using Counter function to
# count frequencies
freq = Counter(str)
# Create a new empty string
res = ""
for i in range(len(str)):
# Append the characters which
# appears more than equal to k times
if (freq[str[i]] >= k):
res += str[i]
return res
# Driver Code
if __name__ == "__main__":
str = "geeksforgeeks"
k = 2
print(removeChars(str, k))
# This code is contributed by vikkycirus
输出:
geeksgeeks