Skip to content

Commit 896bac7

Browse files
author
navneet
committed
GET PERMUTATION AND SUBSEQUENCE OF A GIVEN STRING
1 parent 131a270 commit 896bac7

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// GET PERMUTAION OF A GIVEN STRING
2+
3+
let getPermutation = (str) => {
4+
if (str.length == 1) { // BASE CASE
5+
let array = [];
6+
array.push(str);
7+
return array;
8+
}
9+
10+
let currentCharacter = str.charAt(0);
11+
let restOfString = str.substring(1);
12+
let result = [];
13+
let returnResult = getPermutation(restOfString);
14+
for (j = 0; j < returnResult.length; j++) {
15+
for (i = 0; i <= returnResult[j].length; i++) {
16+
let value = returnResult[j].substring(0, i) + currentCharacter + returnResult[j].substring(i);
17+
result.push(value);
18+
}
19+
}
20+
return result;
21+
}
22+
23+
let permutation = getPermutation('abc');
24+
console.log(permutation);

src/_Problems_/getStringPermutation

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// FIND SUBSEQUENCE OF A GIVEN SUBSTRING
2+
// SUBSTRING OF 'abc' ---->>>> [ '', 'a', 'b', 'ab', 'c', 'ac', 'bc', 'abc' ]
3+
// SUBSTRING OF 'bc' ---->>>> ['', 'b', 'c', 'bc']
4+
// SUBSTRING OF 'c' ---->>>> ['', 'c']
5+
// A pattern can be noticed in above three substrings. Technique followed is recursion.
6+
7+
8+
let getSubesequence = (str) => {
9+
if (str.length == 0) {
10+
let array = [''];
11+
return array;
12+
}
13+
14+
let currentChar = str.charAt(0);
15+
let restOfString = str.substring(1);
16+
17+
let result = [];
18+
let returnResult = getSubesequence(restOfString);
19+
for (i = 0; i < returnResult.length; i++) {
20+
result.push(returnResult[i]);
21+
result.push(currentChar + returnResult[i]);
22+
}
23+
return result;
24+
}
25+
26+
27+
let subsequence = getSubesequence('abc');
28+
console.log(subsequence);

0 commit comments

Comments
 (0)