HackerRank 'Game of Thrones - I' Solution

by Srikant Padala on June 25, 2016, 10:35 am


Explanation


Frequency of atmost one letter should be odd, and frequency every other letter should be even.


Game of Thrones - I Problem Statement

Video

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int hasPallindromicAnagram(string &s) {
    vector<int> freq(26);
    int odd;
    for(int i=0; i < s.size(); i++) {
        freq[s[i]-'a']++;
    }
    for(int i=0; i < 26; i++)
        if(freq[i] % 2) odd++;
    if(odd > 1) return 0;
    else return 1;
}

int main() {  
    string s;
    cin>>s;
    int flag = 0;
    flag = hasPallindromicAnagram(s);
    // Assign Flag a value of 0 or 1 depending on whether or not you find what you are looking for, in the given string 
    if(flag==0)
        cout<<"NO";
    else
        cout<<"YES";
    return 0;
}

Coming Soon.