在这个问题上,我们为用户提供了字符串str。而且我们仅需打印出现频率为奇数的那些字符。
为了解决这个问题,我们必须找到字符串中字符出现的总频率。并且仅打印出现频率奇数的字符串字符。
让我们举个例子来更好地理解这个话题-
Input : adatesaas. Output : dte
说明-出现频率的字符为-
| 一种 | 4 | 
| d | 1 | 
| Ť | 1 | 
| Ë | 1 | 
| s | 2 | 
奇数频率的字符是d,t,e。
现在让我们尝试创建一种算法来解决这个问题-
Step 1 : Traverse the string and count the number of occurrences on characters of the string in an array. Step 2 : Traverse the frequency array and print only those characters whose frequency of occurrence is odd.
让我们基于此算法创建一个程序-
#include <bits/stdc++.h>
using namespace std;
int main(){
   string str = "asdhfjdedsa";
   int n = str.length();
   int frequency[26];
   memset(frequency, 0, sizeof(frequency));
   for (int i = 0; i < n; i++)
      frequency[str[i] - 'a']++;
   for (int i = 0; i < n; i++) {
      if (frequency[str[i] - 'a'] % 2 == 1) {
         cout << str[i]<<" , ";
      }
   }
   return 0;
}输出结果
d , h , f , j , d , e , d