在 codewars 上做一道题,题目如下:
Write function scramble(str1,str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.
For example:
str1 is 'rkqodlw' and str2 is 'world' the output should return true.
str1 is 'cedewaraaossoqqyt' and str2 is 'codewars' should return true.
str1 is 'katas' and str2 is 'steak' should return false.
Only lower case letters will be used (a-z). No punctuation or digits will be included. Performance needs to be considered
我的解法如下:
s2.chars.reject {|c| s1.count(c) >= s2.count(c)}.empty?
虽然通过了测试,但是给出提示运行时间过长,看了别人的答案,有一个如下:
s2.chars.uniq.none? { |c| s1.count(c) < s2.count(c) }
这个是通过了测试,时间也没有问题的
这两种解法区别是在哪里呢?