-
Notifications
You must be signed in to change notification settings - Fork 268
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
896bac7
GET PERMUTATION AND SUBSEQUENCE OF A GIVEN STRING
e06b9d2
followed folder naming convention
51d7b33
--update : delete empty getStringPermutation file
TheSTL 9b687eb
Added get-maze-path problem and added complexities in previous problems
7ba6bfe
Merge branch 'branch1' of https://github.com/navneet15/problem-solvin…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.