原文:

给定一个只包含小写字符的字符串字符串。任务是按照出现的顺序打印具有主要频率的字符。

注意具有主频的重复元素按照其出现的顺序被打印的次数与它们出现的次数一样多。

示例:

输入:输出: gksgks

性格;角色;字母 频率
g′ two
e′ four
k′ two
s two
f′ one
one
r′ one

g ',' k '和' s '是仅有的具有质数频率的字符。 输入:飞机 t4【输出: aeae

方法:创建一个频率数组来存储给定字符串字符串中每个字符的频率。再次遍历字符串,使用厄拉多塞检查该字符的频率是否为质数。

下面是上述方法的实现:

c

// c   implementation of the approach
#include 
using namespace std;
#define size 26
// function to create sieve to check primes
void sieveoferatosthenes(bool prime[], int p_size)
{
    // false here indicates
    // that it is not prime
    prime[0] = false;
    prime[1] = false;
    for (int p = 2; p * p <= p_size; p  ) {
        // if prime[p] is not changed,
        // then it is a prime
        if (prime[p]) {
            // update all multiples of p,
            // set them to non-prime
            for (int i = p * 2; i <= p_size; i  = p)
                prime[i] = false;
        }
    }
}
// function to print the prime frequency characters
// in the order of their occurrence
void printchar(string str, int n)
{
    bool prime[n   1];
    memset(prime, true, sizeof(prime));
    // function to create sieve to check primes
    sieveoferatosthenes(prime, str.length()   1);
    // 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 prime
        if (prime[freq[str[i] - 'a']]) {
            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
class gfg
{
static int size = 26;
// function to create sieve to check primes
static void sieveoferatosthenes(boolean []prime,
                                int p_size)
{
    // false here indicates
    // that it is not prime
    prime[0] = false;
    prime[1] = false;
    for (int p = 2; p * p <= p_size; p  )
    {
        // if prime[p] is not changed,
        // then it is a prime
        if (prime[p])
        {
            // update all multiples of p,
            // set them to non-prime
            for (int i = p * 2; i < p_size; i  = p)
                prime[i] = false;
        }
    }
}
// function to print the prime frequency characters
// in the order of their occurrence
static void printchar(string str, int n)
{
    boolean []prime = new boolean[n   1];
    for(int i = 0; i < n   1; i  )
        prime[i] = true;
    // function to create sieve to check primes
    sieveoferatosthenes(prime, str.length()   1);
    // to store the frequency of each of
    // the character of the string
    int []freq = new int[size];
    // initialize all elements of freq[] to 0
    for(int i =0; i< size; i  )
        freq[i]=0;
    // 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 prime
        if (prime[freq[str.charat(i) - 'a']])
        {
            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 princiraj1992

python 3

# python 3 implementation of the approach
size = 26
from math import sqrt
# function to create sieve to check primes
def sieveoferatosthenes(prime, p_size):
    # false here indicates
    # that it is not prime
    prime[0] = false
    prime[1] = false
    for p in range(2, int(sqrt(p_size)), 1):
        # if prime[p] is not changed,
        # then it is a prime
        if (prime[p]):
            # update all multiples of p,
            # set them to non-prime
            for i in range(p * 2, p_size, p):
                prime[i] = false
# function to print the prime frequency characters
# in the order of their occurrence
def printchar(str, n):
    prime = [true for i in range(n   1)]
    # function to create sieve to check primes
    sieveoferatosthenes(prime, len(str)   1)
    # to store the frequency of each of
    # the character of the string
    freq = [0 for i in range(size)]
    # update the frequency of each character
    for i in range(n):
        freq[ord(str[i]) - ord('a')]  = 1
    # traverse str character by character
    for i in range(n):
        # if frequency of current character is prime
        if (prime[freq[ord(str[i]) - ord('a')]]):
            print(str[i], end = "")
# driver code
if __name__ == '__main__':
    str = "geeksforgeeks"
    n = len(str)
    printchar(str, n)
# this code is contributed by surendra_gangwar

c

// c# implementation of the approach
using system;
class gfg
{
    static int size = 26;
    // function to create sieve to check primes
    static void sieveoferatosthenes(bool[] prime,
                                      int p_size)
    {
        // false here indicates
        // that it is not prime
        prime[0] = false;
        prime[1] = false;
        for (int p = 2; p * p <= p_size; p  )
        {
            // if prime[p] is not changed,
            // then it is a prime
            if (prime[p])
            {
                // update all multiples of p,
                // set them to non-prime
                for (int i = p * 2;
                         i < p_size; i  = p)
                    prime[i] = false;
            }
        }
    }
    // function to print the prime frequency characters
    // in the order of their occurrence
    static void printchar(string str, int n)
    {
        bool[] prime = new bool[n   1];
        for (int i = 0; i < n   1; i  )
            prime[i] = true;
        // function to create sieve to check primes
        sieveoferatosthenes(prime, str.length   1);
        // to store the frequency of each of
        // the character of the string
        int[] freq = new int[size];
        // initialize all elements of freq[] to 0
        for (int i = 0; i < size; i  )
            freq[i] = 0;
        // 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 prime
            if (prime[freq[str[i] - 'a']])
            {
                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
// sanjeev2552

java 描述语言


output

gksgks

方法 2:使用内置函数:

进场:

我们将扫描字符串,并使用内置的 counter()函数计算所有字符的出现次数,然后遍历字符串并检查出现次数是否为质数,如果有任何质数频率,则打印它。

注意:该方法适用于所有类型的字符

下面是上述方法的实现:

java 语言(一种计算机语言,尤用于创建网站)

// java code for the above approach
import java.io.*;
import java.util.*;
class gfg {
    // function to check primes
static boolean prime(int n)
{
    if (n <= 1)
        return false;
    int max_div = (int)math.floor(math.sqrt(n));
    for(int i = 2; i < 1   max_div; i  )
    {
        if (n % i == 0)
            return false;
    }
    return true;
}
static void checkstring(string s)
{
    // counting the frequency of all
    // character using counter function
    map freq = new hashmap();
    for(int i = 0; i < s.length(); i  )
    {
        if (!freq.containskey(s.charat(i)))
            freq.put(s.charat(i),0);
        freq.put(s.charat(i),freq.get(s.charat(i)) 1);
    }
    // traversing string
    for(int i = 0; i < s.length(); i  )
    {
        if (prime(freq.get(s.charat(i))))
            system.out.print(s.charat(i));
    }
}
// driver code
    public static void main (string[] args) {
        string s = "geeksforgeeks";
    // passing string to checkstring function
    checkstring(s);
    }
}
// this code is contributed by avanitrachhadiya2155

python 3

# python code for the above approach
# importing counter function
from collections import counter
import math
# function to check primes
def prime(n):
    if n <= 1:
        return false
    max_div = math.floor(math.sqrt(n))
    for i in range(2, 1   max_div):
        if n % i == 0:
            return false
    return true
def checkstring(s):
    # counting the frequency of all
    # character using counter function
    freq = counter(s)
    # traversing string
    for i in range(len(s)):
        if prime(freq[s[i]]):
            print(s[i], end="")
# driver code
s = "geeksforgeeks"
# passing string to checkstring function
checkstring(s)
# this code is contributed by vikkycirus

c

// c# code for the above approach
using system;
using system.collections.generic;
class gfg{
// function to check primes
static bool prime(int n)
{
    if (n <= 1)
        return false;
    int max_div = (int)math.floor(math.sqrt(n));
    for(int i = 2; i < 1   max_div; i  )
    {
        if (n % i == 0)
            return false;
    }
    return true;
}
static void checkstring(string s)
{
    // counting the frequency of all
    // character using counter function
    dictionary freq = new dictionary();
    for(int i = 0; i < s.length; i  )
    {
        if (!freq.containskey(s[i]))
            freq[s[i]] = 0;
        freq[s[i]]  = 1;
    }
    // traversing string
    for(int i = 0; i < s.length; i  )
    {
        if (prime(freq[s[i]]))
            console.write(s[i]);
    }
}
// driver code
public static void main()
{
    string s = "geeksforgeeks";
    // passing string to checkstring function
    checkstring(s);
}
}
// this code is contributed by ukasp

java 描述语言


output

gksgks