Skip to content

Permutation & Subsequence of String and all paths of M x N grid #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/_Problems_/get-mazePath/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//======================================Problem Statement=============================================
// --->> Print all possible path to reach the end of the GRID/MAZE (N x N) from starting point to ending point
// --->> One horizontal move will be represented by H and one vertical move will be represented by V
// --->> Complexity = Complexity will be exponential as there are many overlapping solutions
// --->> cr = current row
// --->> cc = current column
// --->> er = end row
// --->> ec = end column




let getMazePath = (cr, cc, er, ec) => {
if(cr == er && cc == ec) { //============POSITIVE BASE CASE===========
let br = [];
br.push('');
return br;
}

if(cr > er || cc > ec) { //============NEGATIVE BASE CASE===========
let br = [];
return br;
}

let myResult = [];

let recResultH = getMazePath(cr, cc + 1, er, ec);
recResultH.forEach((rrh) => {
myResult.push("H" + rrh);
});

let recResultV = getMazePath(cr + 1, cc, er, ec);
recResultV.forEach((rrv) => {
myResult.push("V" + rrv);
});

return myResult;
}


let path = getMazePath(0, 0, 2, 2);
console.log(path);
24 changes: 24 additions & 0 deletions src/_Problems_/get_string_permutations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// GET PERMUTAION OF A GIVEN STRING

let getPermutation = (str) => {
if (str.length == 1) { // BASE CASE
let array = [];
array.push(str);
return array;
}

let currentCharacter = str.charAt(0);
let restOfString = str.substring(1);
let result = [];
let returnResult = getPermutation(restOfString);
for (j = 0; j < returnResult.length; j++) {
for (i = 0; i <= returnResult[j].length; i++) {
let value = returnResult[j].substring(0, i) + currentCharacter + returnResult[j].substring(i);
result.push(value);
}
}
return result;
}

let permutation = getPermutation('abc');
console.log(permutation);
30 changes: 30 additions & 0 deletions src/_Problems_/get_subsequence/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// FIND SUBSEQUENCE OF A GIVEN SUBSTRING
// SUBSTRING OF 'abc' ---->>>> [ '', 'a', 'b', 'ab', 'c', 'ac', 'bc', 'abc' ]
// SUBSTRING OF 'bc' ---->>>> ['', 'b', 'c', 'bc']
// SUBSTRING OF 'c' ---->>>> ['', 'c']
// A pattern can be noticed in above three substrings. Technique followed is recursion.
// Time complexity : O(2^n) n is the length of the string provided.


let getSubesequence = (str) => {
if (str.length == 0) {
let array = [''];
return array;
}

let currentChar = str.charAt(0);
let restOfString = str.substring(1);

let result = [];
let returnResult = getSubesequence(restOfString);
for (i = 0; i < returnResult.length; i++) {
result.push(returnResult[i]);
result.push(currentChar + returnResult[i]);
}
return result;
}


let subsequence = getSubesequence('abc');
console.log(subsequence);