Skip to content

Instantly share code, notes, and snippets.

@okovalov
Created February 24, 2019 23:56
Show Gist options
  • Save okovalov/a358c782a31c46e5d0cfcabcfb137cc8 to your computer and use it in GitHub Desktop.
Save okovalov/a358c782a31c46e5d0cfcabcfb137cc8 to your computer and use it in GitHub Desktop.
const harmlessRansomNote = (search, text) => {
let result = true
const textArr = text.toLowerCase().split(' ')
const searchArr = search.toLowerCase().split(' ')
const hash = {}
for (let idx in textArr) {
const word = textArr[idx]
if (!hash[word]) hash[word] = 0
hash[word]++
}
for (let idx in searchArr) {
const word = searchArr[idx]
if (!hash[word] || hash[word] < 1) {
result = false
break
}
hash[word]--
}
return result
}
const search = 'this is so so so'
const text = 'is that this really abc is so so cool vvv'
const result = harmlessRansomNote(search, text)
console.log(`'${search}' is ${!result ? ' not' : '' } fully presented in '${text}', the result is '${result}'`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment