mre / the-coding-interview

Programming exercises, code katas and puzzles for your job interview training - or just for fun.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

can-scramble solutions wrong?

kaido42 opened this issue · comments

at least php-version of the solution fails with testing "aabb" and "aaac" ...

You are right @kaido42. What a shame. 😞
The problem is, that I simply sum up the byte values of the characters. 'a' is 97, 'b' is 98, and 'c' is 99.
Therefore 'a' + 'a' + 'b' + 'b' has the same sum as 'a' + 'a' + 'a+ 'c', which is 390.
What I should have done instead, is to check if the occurrences of each character match.

An O(n) way to do that would be to go over each character in the input one-by-one and increase a counter for the character in an associative array. I do something similar in the Python solution.
Another option would be to sort the inputs and compare the characters from left to right. That would be O(n*log n), though.
Both options would be fine for me.

Could you make a pull request to fix that? Would be great. 😃

Could you make a pull request to fix that? Would be great

sry, too lazy :)

^^ Happy hacktober