From 0474e3e216e34e98749ddfc9fea13a6c65696c08 Mon Sep 17 00:00:00 2001 From: Toihir Halim <66559721+toihirhalim@users.noreply.github.com> Date: Thu, 15 Apr 2021 17:23:25 +0200 Subject: [PATCH] Added string subsequence in JS (#196) * Added string subsequence in js * remove extra line --- strings/README.md | 1 + strings/js/sequence.js | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 strings/js/sequence.js diff --git a/strings/README.md b/strings/README.md index b19e72dd..3544f5a5 100644 --- a/strings/README.md +++ b/strings/README.md @@ -18,6 +18,7 @@ ### JavaScript 1. [Palindrome Check](js/palindrome.js) +2. [All subsequences](js/sequence.js) ### Java diff --git a/strings/js/sequence.js b/strings/js/sequence.js new file mode 100644 index 00000000..ee1c6fd6 --- /dev/null +++ b/strings/js/sequence.js @@ -0,0 +1,44 @@ +var list = []; + +const subsequence = (input, output = '') => { + if (input === "") { + if (output !== '' && !list.includes(output)) list.push(output); + return; + } + + subsequence(input.substr(1), output + input.charAt(0)); + subsequence(input.substr(1), output); +} + +subsequence('abc'); +console.log('abc', list); +list = []; + +subsequence('aaa'); +console.log('aaa', list); +list = []; + +subsequence('hello'); +console.log('hello', list); + +/* + to run the file: + Node sequence.js + + result + abc [ + 'abc', 'ab', + 'ac', 'a', + 'bc', 'b', + 'c' + ] + aaa [ 'aaa', 'aa', 'a' ] + hello [ + 'hello', 'hell', 'helo', 'hel', + 'heo', 'he', 'hllo', 'hll', + 'hlo', 'hl', 'ho', 'h', + 'ello', 'ell', 'elo', 'el', + 'eo', 'e', 'llo', 'll', + 'lo', 'l', 'o' + ] +*/