Day 15: buckle the fifth question, the longest anagram sub string
Address: leetcode-cn.com/problems/lo…
Idea: Central expansion, in fact, and dynamic programming quite like.
var longestPalindrome = function(s) { let result = s[0]; // set the initial value let length = 0; for (let i = 0; i < s.length; i++) { for (let j = 1; j <= 2; j++) { let left = i, right = i + j; While (left >= 0 &&right < s.length &&s [left] == s[right]) {left--, right++; // center extension}; length = right - left - 1; if (length > result.length) { result = s.substr(left + 1, length); }}} return result; };Copy the code
Execution time: 104 ms, beating 93.88% of users in all JavaScript commits
Memory consumption: 40.2 MB, beating 60.89% of all JavaScript commits