LeetCode: Palindromic Substrings Solution
1/**2 * @param {string} s3 * @return {number}4 */5var countSubstrings = function (s) {6 const N = s.length7 const mem = Array.from({ length: N }, _ => Array(N).fill(null))89 const isPalindrome = (i, j) => {10 if (mem[i][j] !== null) {11 return mem[i][j]12 }1314 if (i >= j) {15 return (mem[i][j] = 1)16 }1718 if (s[i] !== s[j]) {19 return (mem[i][j] = 0)20 }2122 return (mem[i][j] = isPalindrome(i + 1, j - 1))23 }2425 let res = 02627 for (let i = 0; i < N; i++) {28 for (let j = i; j < N; j++) {29 res += isPalindrome(i, j) ? 1 : 030 }31 }3233 return res34}
Comments
Loading comments...
Tags
leetcode
string
dynamic programming
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.