Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.
Only lower case letters will be used (a-z). No punctuation or digits will be included. Performance needs to be considered.
scramble('rkqodlw', 'world') ==> True
scramble('cedewaraaossoqqyt', 'codewars') ==> True
scramble('katas', 'steak') ==> False
using DataStructures
using Test
"""
if a portion of str1 characters can be rearranged to match str2
"""
#> "if a portion of str1 characters can be rearranged to match str2\n"
function scramble(str1::String, str2::String)
= DataStructures.counter(str1)
dict1 = DataStructures.counter(str2)
dict2
≤ dict1[key] for key ∈ keys(dict2)] |> all
[dict2[key] end
#> scramble (generic function with 1 method)
@test scramble("rkqodlw", "world") == true
#> Test Passed
#> Expression: scramble("rkqodlw", "world") == true
#> Evaluated: true == true
@test scramble("cedewaraaossoqqyt", "codewars") == true
#> Test Passed
#> Expression: scramble("cedewaraaossoqqyt", "codewars") == true
#> Evaluated: true == true
@test scramble("katas", "steak") == false
#> Test Passed
#> Expression: scramble("katas", "steak") == false
#> Evaluated: false == false
library(tidyverse)
<- function(s1, s2) {
scramble <- s1 %>%
table1 str_split("") %>%
unlist() %>%
table()
<- s2 %>%
table2 str_split("") %>%
unlist() %>%
table()
map_lgl(.x = names(table2), .f = function(key) {
if (!key %in% names(t1)) {
return(FALSE)
else {
} return(table2[key] <= table1[key])
}%>%
}) all()
}