Added string subsequence in JS (#196)
* Added string subsequence in js * remove extra linepull/208/head
parent
ee31aca70b
commit
0474e3e216
|
@ -18,6 +18,7 @@
|
||||||
### JavaScript
|
### JavaScript
|
||||||
|
|
||||||
1. [Palindrome Check](js/palindrome.js)
|
1. [Palindrome Check](js/palindrome.js)
|
||||||
|
2. [All subsequences](js/sequence.js)
|
||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
|
|
|
@ -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'
|
||||||
|
]
|
||||||
|
*/
|
Loading…
Reference in New Issue