Skip to content

Commit d4007e2

Browse files
authored
Merge pull request knaxus#6 from navneet15/branch1
Permutation & Subsequence of String and all paths of M x N grid
2 parents 4ff441b + 7ba6bfe commit d4007e2

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/_Problems_/get-mazePath/index.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//======================================Problem Statement=============================================
2+
// --->> Print all possible path to reach the end of the GRID/MAZE (N x N) from starting point to ending point
3+
// --->> One horizontal move will be represented by H and one vertical move will be represented by V
4+
// --->> Complexity = Complexity will be exponential as there are many overlapping solutions
5+
// --->> cr = current row
6+
// --->> cc = current column
7+
// --->> er = end row
8+
// --->> ec = end column
9+
10+
11+
12+
13+
let getMazePath = (cr, cc, er, ec) => {
14+
if(cr == er && cc == ec) { //============POSITIVE BASE CASE===========
15+
let br = [];
16+
br.push('');
17+
return br;
18+
}
19+
20+
if(cr > er || cc > ec) { //============NEGATIVE BASE CASE===========
21+
let br = [];
22+
return br;
23+
}
24+
25+
let myResult = [];
26+
27+
let recResultH = getMazePath(cr, cc + 1, er, ec);
28+
recResultH.forEach((rrh) => {
29+
myResult.push("H" + rrh);
30+
});
31+
32+
let recResultV = getMazePath(cr + 1, cc, er, ec);
33+
recResultV.forEach((rrv) => {
34+
myResult.push("V" + rrv);
35+
});
36+
37+
return myResult;
38+
}
39+
40+
41+
let path = getMazePath(0, 0, 2, 2);
42+
console.log(path);
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);
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
// Time complexity : O(2^n) n is the length of the string provided.
7+
8+
9+
let getSubesequence = (str) => {
10+
if (str.length == 0) {
11+
let array = [''];
12+
return array;
13+
}
14+
15+
let currentChar = str.charAt(0);
16+
let restOfString = str.substring(1);
17+
18+
let result = [];
19+
let returnResult = getSubesequence(restOfString);
20+
for (i = 0; i < returnResult.length; i++) {
21+
result.push(returnResult[i]);
22+
result.push(currentChar + returnResult[i]);
23+
}
24+
return result;
25+
}
26+
27+
28+
let subsequence = getSubesequence('abc');
29+
console.log(subsequence);
30+

0 commit comments

Comments
 (0)