Good afternoon! Here's our prompt for today.
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)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
45
'PASSED: ' + "`longestPalindrome('nasduhuuhdousan')` should return `'huuh'`"
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');
​
console.log(
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's our guided, illustrated walk-through.
How do I use this guide?