Check Anagram String

PROBLEM :

Given two strings, check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “act” and “tac” are anagram of each other.

Input:

The first line of input contains an integer T denoting the number of test cases. Each test case consist of two strings in 'lowercase' only, in a separate line.

Output:

Print "YES" without quotes if the two strings are anagram else print "NO".

Constraints:

1 = T = 30

1 = |s| = 100

Example:

Input:
2
geeksforgeeks
forgeeksgeeks
allergy
allergic

Output:
YES
NO
     
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<string.h>
bool IsAnagram(char *,char *) ;
int main()
 {
    int t,check ;
    char str1[100],str2[100]  ;
    cin>>t ;
    while(t--)
    {
        cin>>str1 ;
        cin>>str2 ;
       
        check=IsAnagram(str1,str2) ;
        if(check)
            cout<<"YES" ;
        else
            cout<<"NO" ;
        cout<<endl ;
    }
return 0;
}

bool IsAnagram(char *str1,char *str2)
{
    int l1,l2,i ;
    l1=strlen(str1) ;
    l2=strlen(str2) ;
   
    if(l1!=l2)
        return false ;
       
    int hash[256]={0} ;
   
    for(i=0;i<l1;i++)
        hash[str1[i]]=1 ;
       
    for(i=0;i<l2;i++)
    {
        if(hash[str2[i]]==0)
            return false ;
    }
    return true ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )