Skip to content

Commit 57a52aa

Browse files
added subsequenceRecursive.js file under Recursion
1 parent ef5acae commit 57a52aa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Recursive/SubsequenceRecursive.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Problem Statement: Find all distinct, non-empty subsequence of given string in lexicographical order using recursive approach.
3+
*
4+
* What is subsequence?
5+
* A Subsequence is sequence obtained by deleting some or no elements without changing the order of elements
6+
* Example: Given a string = "abcd"
7+
* 1. "abc" is a subsequence
8+
* 2. "abd" is a subsequence
9+
* 3. But "ba" is not a subsequence (because order is changed)
10+
*
11+
* What is lexicographical order?
12+
* In simple terms, lexicographical order is dictionary order.
13+
* Example: Given a string = "abcd"
14+
* 1. "abc" will come before "abcd".
15+
* 2. "abd" will come before "ac".
16+
*
17+
* References for meaning of subsequence & lexicographical:
18+
* https://en.wikipedia.org/wiki/Subsequence
19+
* https://en.wikipedia.org/wiki/Lexicographic_order
20+
*/
21+
22+
const subsequence = (str, seq, low) => {
23+
if (low <= str.length && str.length !== '') {
24+
console.log(seq)
25+
}
26+
for (let i = low; i < str.length; i++) {
27+
subsequence(str, seq + str[i], i + 1)
28+
}
29+
}
30+
31+
const str = 'abcd'
32+
subsequence(str, '', 0)

0 commit comments

Comments
 (0)