内容中心

2026年知名的低压储气罐厂家选择指南-江苏申强特种设备有限公司

江苏申强集团位于敬爱的周恩来总理故乡--淮安。集团占地面积31800平方米,建筑面积15000平方米。现有职工121人,工程技术人员32人,各类生产及检测设备200多套。 集团自创建以来,遵循以人为本,科技为先的发展理念,组建了一支高智科技队伍,研发了一批具有技术的产品。江苏申强特种设备有限公司是压力容器和锅炉制造企业。主要产品:一、二类低、中压容器;储气罐;喷砂罐;隔膜罐;分汽缸;各种非标压力容器;燃油、燃气、燃生物质蒸汽锅炉;常压热水锅炉;除尘设备;换热设备;水处理设备;空气净化器;立体停车场等产品。我们的宗旨是以顾客为中心,以法规为准则,不断创新,持续改进,以放心的质量、优质的服务追求顾客满意。

To solve this problem, we need to count the number of palindromic substrings in a given string. A palindromic substring reads the same forwards and backwards. The efficient approach to solve this problem involves expanding around potential centers of palindromes, which can be either a single character (for odd-length palindromes) or between two characters (for even-length palindromes).

Approach

  1. Expand Around Centers:
    • For each character in the string, consider it as the center of an odd-length palindrome. Expand outwards as long as the characters on both sides are the same, counting valid palindromic substrings.
    • For each pair of consecutive characters, consider them as the center of an even-length palindrome. Expand outwards similarly, counting valid palindromic substrings.
  2. Count Valid Substrings: Sum the counts from both odd and even length palindromes to get the total number of palindromic substrings.

Solution Code

def count_palindromic_substrings(s):
    n = len(s)
    count = 0

    for i in range(n):
        # Check for odd-length palindromes (center at i)
        l, r = i, i
        while l >= 0 and r < n and s[l] == s[r]:
            count += 1
            l -= 1
            r += 1

        # Check for even-length palindromes (center between i and i+1)
        l, r = i, i + 1
        while l >= 0 and r < n and s[l] == s[r]:
            count += 1
            l -= 1
            r += 1

    return count

# Example usage:
# print(count_palindromic_substrings("aaa"))  # Output:6
# print(count_palindromic_substrings("abc"))  # Output:3
# print(count_palindromic_substrings("abba")) # Output:6

Explanation

  • Odd-Length Palindromes: For each character at index i, we start with l = i and r = i (same position). We expand left (l--) and right (r++) as long as s[l] == s[r] and the indices are within bounds. Each valid expansion adds a palindromic substring to the count.
  • Even-Length Palindromes: For each pair of consecutive characters at indices i and i+1, we start with l = i and r = i+1. We expand similarly, checking if the characters are the same and within bounds, adding valid substrings to the count.
  • Time Complexity: The algorithm runs in O(N²) time, where N is the length of the string. This is because for each of the N characters, we expand up to N times in the worst case (e.g., a string of all the same characters).
  • Space Complexity: The algorithm uses O(1) additional space, as it only uses a few variables to track the current expansion and count.

This approach efficiently counts all palindromic substrings by leveraging the symmetry property of palindromes, making it suitable for most practical input sizes.

江苏申强特种设备有限公司

江苏申强特种设备有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?