📌  相关文章
📜  按出现顺序打印具有奇数频率的字符

📅  最后修改于: 2021-10-27 08:10:00             🧑  作者: Mango

给定一个只包含小写字符的字符串str 。任务是按出现的顺序打印具有奇数频率的字符。
注意:具有奇数频率的重复元素按照它们出现的顺序打印它们出现的次数。
例子:

方法:创建一个频率数组来存储给定字符串str的每个字符的频率。再次遍历字符串str并检查该字符的频率是否为奇数。如果是,则打印字符。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define SIZE 26
 
// Function to print the odd 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 odd
        if (freq[str[i] - 'a'] % 2 == 1) {
            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 {
    // Function to print the odd frequency characters
    // in the order of their occurrence
    public static void printChar(String str, int n)
    {
 
        // To store the frequency of each of
        // the character of the string
        int[] freq = new int[26];
 
        // 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 odd
            if (freq[str.charAt(i) - 'a'] % 2 == 1) {
                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 Naman_Garg.


Python3
# Python3 implementation of the approach
import sys
import math
 
# Function to print the odd frequency characters
# in the order of their occurrence
def printChar(str_, n):
 
    # To store the frequency of each of
    # the character of the string and
    # Initialize all elements of freq[] to 0
    freq = [0] * 26
 
    # 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 odd
        if (freq[ord(str_[i]) -
                 ord('a')]) % 2 == 1:
            print("{}".format(str_[i]), end = "")
 
# Driver code
if __name__=='__main__':
    str_ = "geeksforgeeks"
    n = len(str_)
    printChar(str_, n)
 
# This code is contributed by Vikash Kumar 37


C#
// C# implementation of the approach
using System;
 
class GFG {
    // Function to print the odd frequency characters
    // in the order of their occurrence
    public static void printChar(String str, int n)
    {
 
        // To store the frequency of each of
        // the character of the string
        int[] freq = new int[26];
 
        // 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 odd
            if (freq[str[i] - 'a'] % 2 == 1) {
                Console.Write(str[i]);
            }
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String str = "geeksforgeeks";
        int n = str.Length;
        printChar(str, n);
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript


Python3
# importing Counter function
from collections import Counter
 
# function to check if all
# elements occur odd times
def checkString(s):
   
    # Counting the frequency of all
    # character using Counter function
    frequency = Counter(s)
     
    # Traversing frequency
    for i in frequency:
       
        # Checking if any element
        # has even count
        if (frequency[i] % 2 == 0):
            return False
    return True
 
 
# Driver code
s = "gggfffaaa"
if(checkString(s)):
    print("Yes")
else:
    print("No")
     
# This code is contributed by vikkycirus


输出:

for

时间复杂度: O(n)
辅助空间: O(1)

方法#2:使用内置的Python函数。

方法:

我们将扫描字符串并使用内置的Counter()函数计算所有字符的出现次数,然后我们遍历计数器列表并检查出现次数是否为奇数是否有偶数频率字符,然后我们立即打印 No。

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

下面是上述方法的实现:

蟒蛇3

# importing Counter function
from collections import Counter
 
# function to check if all
# elements occur odd times
def checkString(s):
   
    # Counting the frequency of all
    # character using Counter function
    frequency = Counter(s)
     
    # Traversing frequency
    for i in frequency:
       
        # Checking if any element
        # has even count
        if (frequency[i] % 2 == 0):
            return False
    return True
 
 
# Driver code
s = "gggfffaaa"
if(checkString(s)):
    print("Yes")
else:
    print("No")
     
# This code is contributed by vikkycirus

输出:

Yes

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程