-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminWindowSubstring.js
More file actions
35 lines (32 loc) · 1 KB
/
Copy pathminWindowSubstring.js
File metadata and controls
35 lines (32 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function MinWindowSubstring(strArr) {
let str = strArr[0];
let needle = strArr[1].split("");
//start with the smallest possible substrings, then go up
for (let i = needle.length, len = str.length; i <= len; i++) {
for (j = 0; j <= len - i; j++) {
let mySlice = str.substr(j, i);
if (isContained(mySlice)) {
return mySlice;
}
}
}
return "Not in string";
// ---------------------- helpers -----------------------------
//isContained checks to see if all the chars in the needle are in the given string
function isContained(str) {
let arr = str.split("");
for (let i = 0, len = needle.length; i < len; i++) {
let place = arr.findIndex((val) => {
return val === needle[i];
});
if (place === -1) {
return false;
} else {
arr.splice(place, 1);
}
}
return true;
}
}
// keep this function call here
console.log("solution 1: ",MinWindowSubstring(["aaabaaddae", "aed"]));