Longest Palindromic Substring (Hard)
Good afternoon! Here's our prompt for today.
You may see this problem at Facebook, Godaddy, Symantec, Ibm, Glassdoor, Segment, Pipedrive, Servicenow, Cvent, Veeam, Veeva Systems, and Motorola.
We're given a string that's a mixture of several alphabetical characters.
JAVASCRIPT
1const str = "algototheehtotdaily";Could you write a method to find the longest substring that is considered a palindrome? In the above example, it would be "totheehtot".
Where there are multiple longest palindromic substrings of the same length, for example in "abracadabra" ("aca" and "ada"), return the first to appear.
Constraints
- The string can be empty, in which case return an empty string
- The string will consist of only lowercase alphabets
- Expected time complexity :
O(n^2) - Expected space complexity :
O(n^2)
xxxxxxxxxx45
var assert = require('assert');const longestPalindrome = (str) => { // fill in return '';};try { assert.equal(longestPalindrome(''), ''); console.log('PASSED: ' + "`longestPalindrome('')` should return `''`");} catch (err) { console.log(err);}try { assert.equal(longestPalindrome('algodaily'), 'a'); console.log( 'PASSED: ' + "`longestPalindrome('algodaily')` should return `'a'`" );} catch (err) { console.log(err);}try { assert.equal(longestPalindrome('gymmyg'), 'gymmyg');OUTPUT
Results will appear here.