Created
January 25, 2019 04:20
-
-
Save Trshant/8ec3536daf7e09df2207e1c4e06a218a to your computer and use it in GitHub Desktop.
I have been trying to get some kind of intiution in these kind of [problems](https://medium.com/@codingfreak/top-50-dynamic-programming-practice-problems-4208fed71aa3), while i can get the naive solutions done on my own, i cannot think up the solutions easily to the point where i can match the solutions given to these questions in each of those …
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
| function catu(one, two) { | |
| var returnu = []; | |
| two.forEach(element => { | |
| returnu.push(one + element); | |
| returnu.push(element); | |
| }); | |
| return returnu; | |
| } | |
| function main_fun(stringu) { | |
| if (stringu.length == 1) { | |
| return [stringu,'']; | |
| } else { | |
| return catu(stringu[0], main_fun(stringu.slice(1,))); | |
| } | |
| } | |
| // https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb | |
| function reverseString(str) { | |
| if (str === "") | |
| return ""; | |
| else | |
| return reverseString(str.substr(1)) + str.charAt(0); | |
| } | |
| function isPalindrome(inputString) { | |
| if (inputString.length > 2) { | |
| return inputString == reverseString(inputString); | |
| } else { | |
| return false; | |
| } | |
| } | |
| f = main_fun("ABBDCACB"); | |
| g = f.filter(isPalindrome); | |
| g.sort((a, b) => { return b.length - a.length ; }) | |
| console.log("longest palindromic subsequence: ", g[0], " with length of ", g[0].length); |
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
| function longestP(stringInput) { | |
| if (stringInput.length == 0) { | |
| return 0; | |
| } | |
| if (stringInput.length == 1) { | |
| return 1; | |
| } | |
| if (stringInput.slice(0, 1) == stringInput.slice(-1)) { | |
| //console.log(stringInput); | |
| return longestP(stringInput.slice(1, -1))+2; | |
| } | |
| return Math.max(longestP(stringInput.slice(0, -1)), longestP(stringInput.slice(1,))); | |
| } | |
| console.log(longestP("ABBDCACB")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment