diff --git a/src/_Problems_/get-mazePath/index.js b/src/_Problems_/get-mazePath/index.js new file mode 100644 index 00000000..99aead53 --- /dev/null +++ b/src/_Problems_/get-mazePath/index.js @@ -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); \ No newline at end of file diff --git a/src/_Problems_/get_string_permutations/index.js b/src/_Problems_/get_string_permutations/index.js new file mode 100644 index 00000000..94bae13b --- /dev/null +++ b/src/_Problems_/get_string_permutations/index.js @@ -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); \ No newline at end of file diff --git a/src/_Problems_/get_subsequence/index.js b/src/_Problems_/get_subsequence/index.js new file mode 100644 index 00000000..3c0170de --- /dev/null +++ b/src/_Problems_/get_subsequence/index.js @@ -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); +