forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (24 loc) · 850 Bytes
/
index.js
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
// FIND SUBSEQUENCE OF A GIVEN SUBSTRING
// SUBSTRING OF 'abc' ---->>>> [ '', 'a', 'b', 'ab', 'c', 'ac', 'bc', 'abc' ]
// SUBSTRING OF 'bc' ---->>>> ['', 'b', 'c', 'bc']
// SUBSTRING OF 'c' ---->>>> ['', 'c']
// A pattern can be noticed in above three substrings. Technique followed is recursion.
// Time complexity : O(2^n) n is the length of the string provided.
let getSubesequence = (str) => {
if (str.length == 0) {
let array = [''];
return array;
}
let currentChar = str.charAt(0);
let restOfString = str.substring(1);
let result = [];
let returnResult = getSubesequence(restOfString);
for (i = 0; i < returnResult.length; i++) {
result.push(returnResult[i]);
result.push(currentChar + returnResult[i]);
}
return result;
}
module.exports = {
getSubesequence,
};