Valid Anagram

07/16/2016 Hash Table Sort

Question

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.

Note:

You may assume the string contains only lowercase alphabets.

Follow up:

What if the inputs contain unicode characters? How would you adapt your solution to such case?


Solution

Result: Accepted Time: 0 ms

Here should be some explanations.

bool isAnagram(char* s, char* t) {
     int have[256]={0};
     while(*s) have[*(s++)]++;
     while(*t) have[*(t++)]--;
     for(char tmp = 'a';tmp <= 'z'; ++tmp)
         if(have[tmp])
            return false;
     return true;
}

Complexity Analytics

  • Time Complexity:
  • Space Complexity: