var countSubstrings = (s, count = 0) => {
for (let left = 0; (left < s.length); left++) {
for (let right = left; (right < s.length); right++) {
count += Number(isPalindrome(s, left, right));
}
}
return count;
}
const isPalindrome = (s, left, right) => {
while (left < right) {
const isEqual = (s[left] === s[right]);
if (!isEqual) return false;
left++; right--;
}
return true;
}
var countSubstrings = (s, count = 0) => {
const tabu = initTabu(s);
count += singleLetters(s, tabu);
count += doubleLetters(s, tabu);
count += multiLetters(s, tabu);
return count;
};
const initTabu = (s) => new Array(s.length).fill()
.map(() => new Array(s.length).fill(false));
const singleLetters = (s, tabu, count = 0) => {
for (let index = 0; (index < s.length); index++) {
tabu[index][index] = true;
count += Number(tabu[index][index]);
}
return count;
}
const doubleLetters = (s, tabu, count = 0) => {
for (let curr = 0; curr < (s.length - 1); curr++) {
const next = (curr + 1);
const isEqual = (s[curr] === s[next]);
tabu[curr][next] = isEqual;
count += Number(tabu[curr][next]);
}
return count;
}
const multiLetters = (s, tabu, count = 0) => {
for (let window = 3; (window <= s.length); window++) {
count += slideWindow(s, tabu, window);
}
return count;
}
const slideWindow = (s, tabu, window, count = 0) => {
let [ left, right ] = [ 0, (window - 1) ];
while (right < s.length) {
const isTrue = tabu[(left + 1)][(right - 1)];
const isEqual = (s[left] === s[right]);
tabu[left][right] = (isTrue && isEqual);
count += Number(tabu[left][right]);
left++; right++;
}
return count;
}
var countSubstrings = (s, count = 0) => {
for (let i = 0; (i < s.length); i++) {
const [ odd, even ] = [ i, (i + 1) ];
count += isPalindromeFromCenter(s, i, odd);
count += isPalindromeFromCenter(s, i, even);
}
return count;
}
const isPalindromeFromCenter = (s, left, right, count = 0) => {
const isInBounds = () => ((0 <= left) && (right < s.length));
while (isInBounds()) {
const isEqual = (s[left] === s[right]);
if (!isEqual) break;
count++;
left--; right++;
}
return count;
}