Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

647. 回文子串 #99

Open
webVueBlog opened this issue Sep 6, 2022 · 0 comments
Open

647. 回文子串 #99

webVueBlog opened this issue Sep 6, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Owner

647. 回文子串

Description

Difficulty: 中等

Related Topics: 字符串, 动态规划

给你一个字符串 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant