242. Valid Anagram

by

Duct Tape Programmer


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?

Quick Hints

  • Use an array indexed by ascii code

Solution

  • We can use HashTable for unicode charset

Time complexity

O (n + m)

Space complexity

Constant for ascii charset. For unicode charset we will use HashTable and complexity will become O(n)

Notes

comments powered by Disqus