#647. Palindromic Substrings
function countSubstrings(s) {
let count = 0;
const expandAroundCenter = (left, right) => {
while (left >= 0 && right < s.length && s[left] === s[right]) {
count++;
left--;
right++;
}
};
for (let i = 0; i < s.length; i++) {
expandAroundCenter(i, i); // For odd length palindromes
expandAroundCenter(i, i + 1); // For even length palindromes
}
return count;
}
This is actually an interesting question that makes sense.
To get the palindrome of every letter or word in a string, it's important to know that firstly, every letter is a palindrome, next, from this point, which is usually the starting point i.e., first letter, we expand to the right and the left by increasing the right and left pointers, if the value of both right and left pointers are the same, it implies that we found another palindrome, then we continue until the right pointer is greater than the string length and the left pointer less than it.
The solution above will only count the odd length palindrome in an given string, to count the even length string, left pointer will start from index 0 and right pointer, index 0 + 1.
We repeat the same process