Sunday, 17 July 2016

Valid Anagram

/*
    Given two strings s and t, write a function to determine if t is an anagram of s.
    For example, s = "anagram", t = "nagaram", return true
    s = "rat", t = "car", return false.
*/


 #include<iostream>  
 using namespace std;  
 #include<string.h>  
 bool isAnagram(char* s, char* t) {  
   int arr[128] = {0};  
   int i, slen = strlen(s), tlen = strlen(t);  
   if(slen != tlen){  
     return false;  
   }  
   for(i = 0; i < slen; i++){  
     arr[s[i]]++;  
   }  
   for(i = 0; i < tlen; i++){  
     arr[t[i]]--;  
   }  
   for(i = 0; i < 128; i++){  
     if(arr[i]){  
       return false;  
     }  
   }  
   return true;  
 }  
 int main(){  
   char *s = "anagram", *t = "nagaram";  
   cout << isAnagram(s, t);  
   return 0;  
 }  
Share:

0 comments:

Post a Comment

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive