返回介绍

solution / 0600-0699 / 0647.Palindromic Substrings / README_EN

发布于 2024-06-17 01:03:35 字数 4518 浏览 0 评论 0 收藏 0

647. Palindromic Substrings

中文文档

Description

Given a string s, return _the number of palindromic substrings in it_.

A string is a palindrome when it reads the same backward as forward.

A substring is a contiguous sequence of characters within the string.

 

Example 1:

Input: s = "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".

Example 2:

Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of lowercase English letters.

Solutions

Solution 1

class Solution:
  def countSubstrings(self, s: str) -> int:
    ans, n = 0, len(s)
    for k in range(n * 2 - 1):
      i, j = k // 2, (k + 1) // 2
      while ~i and j < n and s[i] == s[j]:
        ans += 1
        i, j = i - 1, j + 1
    return ans
class Solution {
  public int countSubstrings(String s) {
    int ans = 0;
    int n = s.length();
    for (int k = 0; k < n * 2 - 1; ++k) {
      int i = k / 2, j = (k + 1) / 2;
      while (i >= 0 && j < n && s.charAt(i) == s.charAt(j)) {
        ++ans;
        --i;
        ++j;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countSubstrings(string s) {
    int ans = 0;
    int n = s.size();
    for (int k = 0; k < n * 2 - 1; ++k) {
      int i = k / 2, j = (k + 1) / 2;
      while (~i && j < n && s[i] == s[j]) {
        ++ans;
        --i;
        ++j;
      }
    }
    return ans;
  }
};
func countSubstrings(s string) int {
  ans, n := 0, len(s)
  for k := 0; k < n*2-1; k++ {
    i, j := k/2, (k+1)/2
    for i >= 0 && j < n && s[i] == s[j] {
      ans++
      i, j = i-1, j+1
    }
  }
  return ans
}
/**
 * @param {string} s
 * @return {number}
 */
var countSubstrings = function (s) {
  let ans = 0;
  const n = s.length;
  for (let k = 0; k < n * 2 - 1; ++k) {
    let i = k >> 1;
    let j = (k + 1) >> 1;
    while (~i && j < n && s[i] == s[j]) {
      ++ans;
      --i;
      ++j;
    }
  }
  return ans;
};

Solution 2

class Solution:
  def countSubstrings(self, s: str) -> int:
    t = '^#' + '#'.join(s) + '#$'
    n = len(t)
    p = [0 for _ in range(n)]
    pos, maxRight = 0, 0
    ans = 0
    for i in range(1, n - 1):
      p[i] = min(maxRight - i, p[2 * pos - i]) if maxRight > i else 1
      while t[i - p[i]] == t[i + p[i]]:
        p[i] += 1
      if i + p[i] > maxRight:
        maxRight = i + p[i]
        pos = i
      ans += p[i] // 2
    return ans
class Solution {
  public int countSubstrings(String s) {
    StringBuilder sb = new StringBuilder("^#");
    for (char ch : s.toCharArray()) {
      sb.append(ch).append('#');
    }
    String t = sb.append('$').toString();
    int n = t.length();
    int[] p = new int[n];
    int pos = 0, maxRight = 0;
    int ans = 0;
    for (int i = 1; i < n - 1; i++) {
      p[i] = maxRight > i ? Math.min(maxRight - i, p[2 * pos - i]) : 1;
      while (t.charAt(i - p[i]) == t.charAt(i + p[i])) {
        p[i]++;
      }
      if (i + p[i] > maxRight) {
        maxRight = i + p[i];
        pos = i;
      }
      ans += p[i] / 2;
    }
    return ans;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文