for within str1 in any character order of appearance.
could someone help me understand why would this be an unoptimized approach, or what could be done to optimize it?
It is currently timing out on the test platform for tests with "very long strings".
Help appreciated :)
function scramble(str1, str2) { const dataArray = str1.split('').sort(); const searchStringArray = str2.split('').sort(); return searchStringArray.every((letter)=> { const searchLetterOccurrences = getOccurrences(letter, searchStringArray); const hashLetterOccurrences = getOccurrences(letter, dataArray); return (hashLetterOccurrences >= searchLetterOccurrences); }); function getOccurrences(letter, array) { const first = array.indexOf(letter); const last = array.lastIndexOf(letter); const occurences = first == -1 ? 0 : first == 0 ? last + 1 : last - first + 1; return occurences; } }
Hope that's ok to paste in like this
Обсуждают сегодня