Ad
  • Default User Avatar

    Good solution, at the cost of memory. It's always a time/memory tradeoff.

  • Custom User Avatar

    The thing with the index trick is that you don't find them in an array - you find them in the object you create at the key of (lettersinanagramsorted).. so your total O() for the algo is A:(Time to Create and populate the index) + B:(time to sort and find one word in the index). For A:, you have to re-sort the letter in the word (of size k), (n) times - so that == n x sortletters(k) == n x klogk [assuming k is sorted using MergeSort, which I think V8 does and the avg. time of MergeSort.]. B: == (not sure exactly, but pretty sure it's less than k) == k .: O() == nklogk + k, so it should be better than n^2. If you can pre-populate the index, you can cut it down to just k. This is a good Kata to know, because you will get this one in phone interviews sometimes... (I got this exact question pretty much on the last phone screen I did.)

  • Custom User Avatar

    Awesome kata - I think if you use the index trick a lot of people used, you get it faster than that.