File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 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 !== 0 ) {
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 )
You can’t perform that action at this time.
0 commit comments