原文:

给定仅包含小写字符的字符串str。 任务是按照出现的频率打印出具有奇数频率的字符。

注意:具有奇数频率的重复元素按出现的次数打印多次。

示例

输入str = "geeksforgeeks"

输出for

字符 频率
g 2
e 4
k 2
s 2
f 1
o 1
r 1

for是唯一具有奇数频率的字符。

输入str = "elephant"

输出lphant

方法:创建一个频率数组,以存储给定字符串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 

输出

for

时间复杂度o(n)

辅助空间o(1)