From dd119f8781e4586df3b2c3661c761ef397a2f4eb Mon Sep 17 00:00:00 2001 From: Jeremy Wohltman Date: Sat, 16 Oct 2021 10:44:56 -0500 Subject: [PATCH 1/2] Create ShortestSubstring.js --- DSA/Strings/ShortestSubstring.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 DSA/Strings/ShortestSubstring.js diff --git a/DSA/Strings/ShortestSubstring.js b/DSA/Strings/ShortestSubstring.js new file mode 100644 index 0000000..2335f0e --- /dev/null +++ b/DSA/Strings/ShortestSubstring.js @@ -0,0 +1,24 @@ +const compareSets = (a, b) => a.size === b.size && [...a].every(e => b.has(e)) + +function shortestSubstring(s) { + let len = s.length + let uniqueChars = new Set(Array.from(s)) + let subString = '' + let mLen = len + 1; + + for (let i = 0; i < len; i++) { + for (let j = i; j < len; j++) { + subString = subString + s[j] + if (compareSets(new Set(subString), uniqueChars)) { + if (mLen > subString.length) { + mLen = subString.length + } + break; + } + } + subString = '' + } + return mLen +} + +console.log(shortestSubstring('bcaacbc')) From 4b9dfa913c13e19b3575501a146377f7f10663f2 Mon Sep 17 00:00:00 2001 From: Jeremy Wohltman Date: Sat, 16 Oct 2021 10:46:09 -0500 Subject: [PATCH 2/2] Create SmallestCommonMultiple.js --- DSA/Miscelleneous/SmallestCommonMultiple.js | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 DSA/Miscelleneous/SmallestCommonMultiple.js diff --git a/DSA/Miscelleneous/SmallestCommonMultiple.js b/DSA/Miscelleneous/SmallestCommonMultiple.js new file mode 100644 index 0000000..b18d7e8 --- /dev/null +++ b/DSA/Miscelleneous/SmallestCommonMultiple.js @@ -0,0 +1,36 @@ +/* + +Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by +all sequential numbers in the range between these parameters. + +The range will be an array of two numbers that will not necessarily be in numerical order. + +For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible +by all numbers between 1 and 3. The answer here would be 6. + +*/ + +function smallestCommons(arr) { + var range = []; + for (var i = Math.max(arr[0], arr[1]); i >= Math.min(arr[0], arr[1]); i--) { + range.push(i); + } + + // could use reduce() in place of this block + var lcm = range[0]; + for (i = 1; i < range.length; i++) { + var GCD = gcd(lcm, range[i]); + lcm = (lcm * range[i]) / GCD; + } + return lcm; + + function gcd(x, y) { + if (y === 0) + return x; + else + return gcd(y, x%y); + } +} + +// test here +smallestCommons([1,5]);