From 75862acdaaa973870a954f54190706a3abb3b554 Mon Sep 17 00:00:00 2001 From: Reece Kidd Date: Thu, 18 Mar 2021 16:22:54 +0000 Subject: [PATCH 1/3] feat: added new urlify method --- chapter01/1.3 - URLify/urlify-4.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 chapter01/1.3 - URLify/urlify-4.js 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"); From bdeaba82c48a4458a13ca4db8da3556e9a29c9f2 Mon Sep 17 00:00:00 2001 From: Reece Kidd Date: Fri, 19 Mar 2021 08:31:50 +0000 Subject: [PATCH 2/3] added checkPermute variation that does not require sorting --- chapter01/1.2 - Check Perm/checkPermute-2.js | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 chapter01/1.2 - Check Perm/checkPermute-2.js 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 ")); From 743252d7b6377bf32d12fe2c59f6c7abb2c364dd Mon Sep 17 00:00:00 2001 From: Reece Kidd Date: Fri, 19 Mar 2021 08:34:05 +0000 Subject: [PATCH 3/3] added isUnique 2 variation --- chapter01/1.1 - Is Unique/isUnique-2.js | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 chapter01/1.1 - Is Unique/isUnique-2.js 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(""));