-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting-string-subsequences.js
More file actions
33 lines (31 loc) · 1022 Bytes
/
Copy pathcounting-string-subsequences.js
File metadata and controls
33 lines (31 loc) · 1022 Bytes
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
/*
Counting String Subsequences kata
https://www.codewars.com/kata/52f7892a747862fc9a0009a6/train/javascript
*/
function countSubsequences(needle, haystack) {
if (needle === '') return 1;
if (haystack === '') return 0;
let total = 0;
for (let index = 0; index < haystack.length; index ++){
if (needle[0] === haystack[index]) {
total += countSubsequences(needle.slice(1), haystack.slice(index+1))
}
}
return total;
}
let cache = {};
function countSubsequences(needle, haystack, root=true) {
if (root) cache = {};
if (needle === '') return 1;
if (haystack === '') return 0;
let total = 0;
for (let index = 0; index < haystack.length; index ++){
if (needle[0] === haystack[index]) {
const cached_result = cache[`${needle.slice(1)}-${haystack.slice(index+1)}`];
if (cached_result) total += cached_result;
else total += countSubsequences(needle.slice(1), haystack.slice(index+1), root=false);
}
}
cache[`${needle}-${haystack}`] = total;
return total;
}