HackerRank 'Anagram' Solution

by Srikant Padala on June 25, 2016, 4:27 am


Explanation


  • Split the string into two equal strings.
  • Sort the two strings individually (Prerequisite to use set_difference function).
  • Perform set difference to find the number of characters to be converted to make both the strings anagrams.

Anagram Problem Statement

Video

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

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int t;
    cin >> t;
    string input;
    while(t--) {
        cin >> input;
        string a = input.substr(0,input.size()/2);
        string b = input.substr(input.size()/2);
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        string c;
        if(a.size() == b.size()) {
            auto it = set_difference(a.begin(), a.end(),b.begin(), b.end(), c.begin());
            c.resize(it-c.begin());
            cout << c.size() << endl;
        }
        else cout << -1 << endl;
    }
    return 0;
}

Coming Soon.