647. 回文子串
webVueBlog opened this issue · comments
I am ne zha / Jeskson commented
647. 回文子串
Description
Difficulty: 中等
给你一个字符串 s
,请你统计并返回这个字符串中 回文子串 的数目。
回文字符串 是正着读和倒过来读一样的字符串。
子字符串 是字符串中的由连续字符组成的一个序列。
具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。
示例 1:
输入:s = "abc"
输出:3
解释:三个回文子串: "a", "b", "c"
示例 2:
输入:s = "aaa"
输出:6
解释:6个回文子串: "a", "a", "a", "aa", "aa", "aaa"
提示:
1 <= s.length <= 1000
s
由小写英文字母组成
Solution
Language: JavaScript
/**
* @param {string} s
* @return {number}
*/
var countSubstrings = function(s) {
let count = 0
for (let i = 0; i < s.length; i++) {
let s1 = '', s2 = ''
for (let j = i; j < s.length; j++) {
s1 = s1 + s[j], s2 = s[j] + s2;
if (s1 === s2) count++
}
}
return count
}