MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / scramble

Function scramble

strings/scramble.js:9–28  ·  view source on GitHub ↗
(str1, str2)

Source from the content-addressed store, hash-verified

7// O(n) time - where n is the total of chars in str1
8// O(1) space - because at maximum we will have 26 letters in our dictionary
9function scramble(str1, str2) {
10 const frequencies = {};
11
12 for (const char of str1) {
13 if (char in frequencies) {
14 frequencies[char] += 1;
15 } else {
16 frequencies[char] = 1;
17 }
18 }
19
20 for (const char of str2) {
21 if (frequencies.hasOwnProperty(char) && frequencies[char] > 0) {
22 frequencies[char]--;
23 } else {
24 return false;
25 }
26 }
27 return true;
28}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected