You may also have seen this problem in interview questions. Two cases:
1. all chars have even numbers of it
2. only 1 char have odd numbers of it
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string s; cin>>s; int flag = 0; // Assign Flag a value of 0 or 1 depending on whether or not you find what you are looking for, in the given string int rec[26] = {0}; for(auto c : s) rec[c - 'a'] ++; int oddCnt = 0; for(int i = 0; i < 26; i ++) oddCnt += rec[i] % 2 ? 1 : 0; flag = oddCnt <= 1 ? 1 : 0; if(flag==0) cout<<"NO"; else cout<<"YES"; return 0; }