Skip to content

Instantly share code, notes, and snippets.

@runandrerun
Created December 16, 2018 21:18
Show Gist options
  • Save runandrerun/7f2d0263d5fff6b6a48afbf101351a52 to your computer and use it in GitHub Desktop.
Save runandrerun/7f2d0263d5fff6b6a48afbf101351a52 to your computer and use it in GitHub Desktop.
const dictionaryMatch = (stringA, stringB) => {
// create an empty dictionary to store your string
const dictionary = {};
// split stringA by character and iterate over it
// create a new key value pair in the dictionary
// for each unique character within the array
stringA.split('').forEach(character => {
dictionary[character] = character;
});
// perform a lookup in the dictionary:
// iterate over stringB, and check to see if the key
// exists in the dictionary we just created
const bLength = stringB.length;
for (let i = 0; i < bLength; i++) {
if (dictionary[stringB[i]] === stringB[i]) {
return true;
}
};
// if the key does not exist then return false
return false;
};
const HELLO = "Hello";
const GOODBYE = "Goodbye";
dictionaryMatch(HELLO, GOODBYE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment