diff --git a/chapter01/1.1 - Is Unique/isUnique-2.js b/chapter01/1.1 - Is Unique/isUnique-2.js new file mode 100644 index 0000000..76313a1 --- /dev/null +++ b/chapter01/1.1 - Is Unique/isUnique-2.js @@ -0,0 +1,28 @@ +const isUniqueCharacters = (input) => { + if (input.length === 0) { + return true; + } + // Based on ASCII string + if (input.length > 128) { + return false; + } + + const uniqueCharacters = {}; + + for (i = 0; i < input.length; i++) { + const character = input[i]; + if (uniqueCharacters[character]) { + return false; + } + + if (!uniqueCharacters[character]) { + uniqueCharacters[character] = 1; + } + } + + return true; +}; + +console.log(isUniqueCharacters("abcde")); +console.log(isUniqueCharacters("abcdefghh")); +console.log(isUniqueCharacters("")); diff --git a/chapter01/1.2 - Check Perm/checkPermute-2.js b/chapter01/1.2 - Check Perm/checkPermute-2.js new file mode 100644 index 0000000..f110fac --- /dev/null +++ b/chapter01/1.2 - Check Perm/checkPermute-2.js @@ -0,0 +1,33 @@ +const checkPermute = (stringA, stringB) => { + if (stringA.length !== stringB.length) { + return false; + } + const letters = {}; + for (i = 0; i < stringA.length; i++) { + const letter = stringA[i]; + if (!letters[letter]) { + letters[letter] = 1; + } else { + letters[letter] += 1; + } + } + + for (i = 0; i < stringB.length; i++) { + const letter = stringB[i]; + if (!letters[letter]) { + return false; + } + letters[letter] -= 1; + + if (letters[letter] < 0) { + return false; + } + } + + return true; +}; + +console.log(checkPermute("a", "bbbb")); +console.log(checkPermute("aba", "baa")); +console.log(checkPermute("aaaa", "bbbb")); +console.log(checkPermute(" a", "a ")); diff --git a/chapter01/1.3 - URLify/urlify-4.js b/chapter01/1.3 - URLify/urlify-4.js new file mode 100644 index 0000000..752620a --- /dev/null +++ b/chapter01/1.3 - URLify/urlify-4.js @@ -0,0 +1,10 @@ +const urlify = (input) => { + return [...input].reduce((previousValue, character) => { + if (character === " ") { + return (previousValue += "%20"); + } + return (previousValue += character); + }, ""); +}; + +console.log(urlify("Mr John Smith"), "Mr%20John%20Smith");