Leetcode 647. Palindromic Substrings

Given a string, your task is to count how many palindromic substrings in this string.

The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

Example 1:
Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Note:
The input string length won't exceed 1000.

int parlin[1001][1001];
int countSubstrings(char* s) {
    int length=0,ans=0;
    while(s[length]!='\0')
    {
        length++;
    }
    ans=length;
    for(int i=0;i<length;i++)
    {
        for(int j=0;j<length;j++)
            parlin[i][j]=0;
    }
    for(int i=0;i<length;i++)
    {
        parlin[i][i]=1;
    }
    for(int i=0;i<length-1;i++)
    {
        if(s[i]==s[i+1])
        {
            parlin[i][i+1]=1;
            ans++;
        }
    }
    for(int i=2;i<length;i++)
    {
        for(int j=0;j<length-i;j++)
        {
            if(s[j]==s[j+i]&&parlin[j+1][j+i-1]==1)
            {
                parlin[j][i+j]=1;
                ans++;
            }
        }
    }
    return ans;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • Introduction What is Bowtie 2? Bowtie 2 is an ultrafast a...
    wzz阅读 5,816评论 0 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 在正月十五傍晚来临,爸爸说:要带着妈妈和我一起去广场 看花灯,我高兴地上窜下跳。 ...
    许慧云阅读 150评论 0 0
  • 今天遇到了很多想不到的是。也想到了爸爸妈妈的难处,过
    马金龙阅读 210评论 0 0