Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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;
}
posted on 2015-02-27 07:05  Tonix  阅读(167)  评论(0编辑  收藏  举报