-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscramblies.js
28 lines (25 loc) · 932 Bytes
/
scramblies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.
// Notes:
// Only lower case letters will be used (a-z). No punctuation or digits will be included.
// Performance needs to be considered.
// Examples
// scramble('rkqodlw', 'world') ==> True
// scramble('cedewaraaossoqqyt', 'codewars') ==> True
// scramble('katas', 'steak') ==> False
function scramble(str1, str2) {
let index = 0;
let len = str2.length;
let savedIndexes = {};
let isMatch = false;
while (index < len) {
let letter = str2[index];
let startingLetterIndex = (savedIndexes[letter] + 1) || 0;
let matchIndex = str1.indexOf(letter, startingLetterIndex);
isMatch = matchIndex > -1;
if (!isMatch)
break;
index++;
savedIndexes[letter] = matchIndex;
}
return isMatch;
}