Find if 2 strings are anagram in O(1) space and O(n) time

shreyasva picture shreyasva · Jul 11, 2011 · Viewed 8.9k times · Source

You can find if 2 strings are anagrams after sorting both strings in O(nlogn) time, however is it possible to find it in o(n) time and O(1) space.

Answer

NPSF3000 picture NPSF3000 · Jul 11, 2011

Absolutely no expert here...

But why not go through each string and simply count how many times each letter turns up.

Given appropriate implementation, this shouldn't take more than O(n) time.