Last active
June 4, 2018 16:15
-
-
Save wesleyduff/a791c784b07b58d0373a92cc7c7a1b16 to your computer and use it in GitHub Desktop.
Get Longest Palindrome from a string using JavaScript : Node.js
This file contains 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
/** | |
* Dynamic Programming example : | |
* break the problems down into individual methods that handle one thing | |
* Put together the solutions from all of the individual methods to come up with the answer. | |
* store | |
* - table bool [][] // storage data structure | |
* - palindrome | |
* - length of pongest palindrome | |
* | |
* methods: sub problems | |
* set all values to false - to start | |
* set all single letters to true | |
* set all doule letters that are the same to true or else false | |
* set table cell to true if | |
* first and last letters in substring are the same | |
* table[i+1, j-1] is true | |
*/ | |
class Palindrome { | |
constructor(chars){ | |
this.palindrome = chars; | |
this.table = new Object(); | |
this.longestPalindrome = null; | |
this.longestPalindromeLength = 0; | |
if(!this.isTheStringAPalindrome()){ | |
this.initialSetupOfTableStructure(); | |
} | |
} | |
isTheStringAPalindrome(){ | |
const reverse = [...this.palindrome].reverse().join(''); | |
if(this.palindrome === reverse){ | |
this.longestPalindrome = this.palindrome; | |
this.longestPalindromeLength = this.palindrome.length; | |
console.log('pal is longest', ); | |
return true; | |
} | |
} | |
initialSetupOfTableStructure(){ | |
for(let i = 0; i < this.palindrome.length; i++){ | |
for(let k = 0; k < this.palindrome.length; k++){ | |
this.table[`${i},${k}`] = false; | |
} | |
} | |
this.setIndividualsAsPalindromes(); | |
} | |
setIndividualsAsPalindromes(){ | |
for(let i = 0; i < this.palindrome.length; i++){ | |
this.table[`${i},${i}`] = true; | |
} | |
this.setDoubleLettersPlaindrome(); | |
} | |
setDoubleLettersPlaindrome(){ | |
for(let i = 0; i < this.palindrome.length; i++){ | |
const firstSubstring = this.palindrome.substring(i, i + 1); | |
const secondSubstring = this.palindrome.substring(i+1, i + 2); | |
if(firstSubstring === secondSubstring){ | |
this.table[`${i},${i + 1}`] = true; | |
if(this.longestPalindromeLength < 2){ | |
this.longestPalindrome = firstSubstring + secondSubstring; | |
this.longestPalindromeLength = 2; | |
} | |
} | |
} | |
this.setAnyPalindromLengthGreaterThan2(); | |
} | |
setAnyPalindromLengthGreaterThan2(){ | |
for(let k = 3; k <= this.palindrome.length; k++){ | |
for(let i = 0; i <= this.palindrome.length - k; i++){ | |
const j = i + k - 1; | |
const tableAtIJ = this.table[`${i+1},${j-1}`]; | |
const stringToCompare = this.palindrome.substring(i, j +1); | |
const firstLetterInstringToCompare = stringToCompare[0]; | |
const lastLetterInstringToCompare = [...stringToCompare].reverse()[0]; | |
if(tableAtIJ && firstLetterInstringToCompare === lastLetterInstringToCompare){ | |
this.table[`${i},${j}`] = true; | |
if(this.longestPalindromeLength < stringToCompare.length){ | |
this.longestPalindrome = stringToCompare; | |
this.longestPalindromeLength = stringToCompare.length; | |
} | |
} | |
} | |
} | |
} | |
printLongestPalindrome(){ | |
console.log('Logest Palindrome', this.longestPalindrome); | |
console.log('from /n', this.palindrome ); | |
} | |
toString(){ | |
console.log('palindrome', this.palindrome); | |
console.log(this.table) | |
} | |
} | |
// const palindrome = new Palindrome('lollolkidding'); | |
// const palindrome = new Palindrome('acbaabca'); | |
const palindrome = new Palindrome('acbaabad'); | |
palindrome.printLongestPalindrome(); | |
//palindrome.toString(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment