CodeChef : Anagram (TECH04)


Title : Anagram

Link : http://www.codechef.com/problems/TECH04/

Problem code: TECH04

Tricky Lines :

  1. The strings would consist of only letters ‘a’-‘z’
  2. You have to print “YES” if one string is an anagram of the other or “NO” otherwise.

Analysis :

  1. Data type : string / char array
  2. sort the two input string
  3. now if strings are exactly same then output “YES”

Runtime : 0.02s

Critical Input : 
5
boom bmmo
aaab baab
tuple tuple
forget forgeo
tuple tuples

output :
NO
NO
YES
NO
NO

Solution :

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define N 1000000
#define LL long long
using namespace std;

int main ()
{
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        string first, second;
        cin >> first >> second;

        sort (first.begin (), first.end ());
        sort (second.begin (), second.end ());

        if ( first == second ) printf ("YES\n");
        else printf ("NO\n");
    }

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s