Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 89ad6ba

Browse filesBrowse files
authored
Algorithm for Misc and Strings (codesONLY#57)
* Create ShortestSubstring.js * Create SmallestCommonMultiple.js
1 parent a34700c commit 89ad6ba
Copy full SHA for 89ad6ba

File tree

Expand file treeCollapse file tree

2 files changed

+60
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+60
-0
lines changed
Open diff view settings
Collapse file
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by
4+
all sequential numbers in the range between these parameters.
5+
6+
The range will be an array of two numbers that will not necessarily be in numerical order.
7+
8+
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible
9+
by all numbers between 1 and 3. The answer here would be 6.
10+
11+
*/
12+
13+
function smallestCommons(arr) {
14+
var range = [];
15+
for (var i = Math.max(arr[0], arr[1]); i >= Math.min(arr[0], arr[1]); i--) {
16+
range.push(i);
17+
}
18+
19+
// could use reduce() in place of this block
20+
var lcm = range[0];
21+
for (i = 1; i < range.length; i++) {
22+
var GCD = gcd(lcm, range[i]);
23+
lcm = (lcm * range[i]) / GCD;
24+
}
25+
return lcm;
26+
27+
function gcd(x, y) {
28+
if (y === 0)
29+
return x;
30+
else
31+
return gcd(y, x%y);
32+
}
33+
}
34+
35+
// test here
36+
smallestCommons([1,5]);
Collapse file

‎DSA/Strings/ShortestSubstring.js‎

Copy file name to clipboard
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const compareSets = (a, b) => a.size === b.size && [...a].every(e => b.has(e))
2+
3+
function shortestSubstring(s) {
4+
let len = s.length
5+
let uniqueChars = new Set(Array.from(s))
6+
let subString = ''
7+
let mLen = len + 1;
8+
9+
for (let i = 0; i < len; i++) {
10+
for (let j = i; j < len; j++) {
11+
subString = subString + s[j]
12+
if (compareSets(new Set(subString), uniqueChars)) {
13+
if (mLen > subString.length) {
14+
mLen = subString.length
15+
}
16+
break;
17+
}
18+
}
19+
subString = ''
20+
}
21+
return mLen
22+
}
23+
24+
console.log(shortestSubstring('bcaacbc'))

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.