原文:
给定仅包含小写字符的字符串str
。 任务是按照出现的顺序打印偶数频率的字符。
注意:重复出现的偶数频率按出现的次数打印。
示例:
输入:
str = "geeksforgeeks"
输出:
geeksgeeks
字符 频率 g
2 e
4 k
2 s
2 f
1 o
1 r
1 仅有
g
,e
,k
和s
是频率偶数的字符。输入:
str = "aeroplane"
输出:
aeae
方法:创建一个频率数组,以存储给定字符串str
的每个字符的频率。 再次遍历字符串str
,并检查该字符的频率是否为偶数。 如果是,则打印字符。
下面是上述方法的实现:
c
// c implementation of the approach
#include
using namespace std;
#define size 26
// function to print the even frequency characters
// in the order of their occurrence
void printchar(string str, int n)
{
// to store the frequency of each of
// the character of the string
int freq[size];
// initialize all elements of freq[] to 0
memset(freq, 0, sizeof(freq));
// update the frequency of each character
for (int i = 0; i < n; i )
freq[str[i] - 'a'] ;
// traverse str character by character
for (int i = 0; i < n; i ) {
// if frequency of current character is even
if (freq[str[i] - 'a'] % 2 == 0) {
cout << str[i];
}
}
}
// driver code
int main()
{
string str = "geeksforgeeks";
int n = str.length();
printchar(str, n);
return 0;
}
java
// java implementation of the approach
import java.util.*;
class gfg
{
static int size = 26;
// function to print the even frequency characters
// in the order of their occurrence
static void printchar(string str, int n)
{
// to store the frequency of each of
// the character of the string
int []freq = new int[size];
// update the frequency of each character
for (int i = 0; i < n; i )
freq[str.charat(i) - 'a'] ;
// traverse str character by character
for (int i = 0; i < n; i )
{
// if frequency of current character is even
if (freq[str.charat(i) - 'a'] % 2 == 0)
{
system.out.print(str.charat(i));
}
}
}
// driver code
public static void main(string[] args)
{
string str = "geeksforgeeks";
int n = str.length();
printchar(str, n);
}
}
// this code is contributed by 29ajaykumar
python3
# python3 implementation of the approach
size = 26
# function to print the even frequency characters
# in the order of their occurrence
def printchar(string, n):
# to store the frequency of each of
# the character of the stringing
# initialize all elements of freq[] to 0
freq = [0] * size
# update the frequency of each character
for i in range(0, n):
freq[ord(string[i]) - ord('a')] = 1
# traverse string character by character
for i in range(0, n):
# if frequency of current character is even
if (freq[ord(string[i]) -
ord('a')] % 2 == 0):
print(string[i], end = "")
# driver code
if __name__ == '__main__':
string = "geeksforgeeks"
n = len(string)
printchar(string, n)
# this code is contributed by ashutosh450
c
// c# implementation of the approach
using system;
using system.collections.generic;
class gfg
{
static int size = 26;
// function to print the even frequency characters
// in the order of their occurrence
static void printchar(string str, int n)
{
// to store the frequency of each of
// the character of the string
int []freq = new int[size];
// update the frequency of each character
for (int i = 0; i < n; i )
freq[str[i] - 'a'] ;
// traverse str character by character
for (int i = 0; i < n; i )
{
// if frequency of current character is even
if (freq[str[i] - 'a'] % 2 == 0)
{
console.write(str[i]);
}
}
}
// driver code
public static void main(string[] args)
{
string str = "geeksforgeeks";
int n = str.length;
printchar(str, n);
}
}
// this code is contributed by princi singh
输出:
geeksgeeks
时间复杂度:o(n)
。
辅助空间:o(1)
。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处