LeetCode 17  Valid Anagram
Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s.


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


Given the assumptions that the strings will only be lowercase, you can get the index of each character in the array by subtracting the ord(char)
by 97 which is the ordinal value of a
. Then you loop through one of the strings, add and then subtract each index. Finally you loop through the 26 length char array and as soon as you encounter an index that isn’t 0 then the strings aren’t anagrams.
Time complexity O(n)
Space complexity O(1)
because 26 char array doesn’t grow with size.
For unicode followup, you can use a hash table.