forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
28 lines (24 loc) · 832 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
// 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.
function getSubesequence(str) {
if (str.length === 0) {
const array = [''];
return array;
}
const currentChar = str.charAt(0);
const restOfString = str.substring(1);
const result = [];
const returnResult = getSubesequence(restOfString);
for (let i = 0; i < returnResult.length; i += 1) {
result.push(returnResult[i]);
result.push(currentChar + returnResult[i]);
}
return result;
}
module.exports = {
getSubesequence,
};