# LeetCode: Palindromic Substrings Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**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))8
9  const isPalindrome = (i, j) => {10    if (mem[i][j] !== null) {11      return mem[i][j]12    }13
14    if (i >= j) {15      return (mem[i][j] = 1)16    }17
18    if (s[i] !== s[j]) {19      return (mem[i][j] = 0)20    }21
22    return (mem[i][j] = isPalindrome(i + 1, j - 1))23  }24
25  let res = 026
27  for (let i = 0; i < N; i++) {28    for (let j = i; j < N; j++) {29      res += isPalindrome(i, j) ? 1 : 030    }31  }32
33  return res34}```

## Tags

leetcode

string

dynamic programming

## Next Post

LeetCode: Evaluate The Bracket Pairs Of A String

Mar 28, 2021

Search Posts