回文子串

此博客链接:https://www.cnblogs.com/ping2yingshi/p/14335083.html

回文子串

题目链接:https://leetcode-cn.com/problems/palindromic-substrings/

题目

给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。

具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。

示例 1:

输入:"abc"
输出:3
解释:三个回文子串: "a", "b", "c"
示例 2:

输入:"aaa"
输出:6
解释:6个回文子串: "a", "a", "a", "aa", "aa", "aaa"

题解

 先把字符串转成字符数组,然后把数组中的相同的元素下标当成value,数组元素当成key,一个字符串的子串由以下三种情况组成:

1.判断统计每个key对应的value和有多少个(因为不同位置的相同字母算不同的子串,所以这里可以利用组合数求出一共有多少中可能性。

2.一共有集中不同的字母。

3.不同字母中,如果某个字母有超过两个字母并且下标第一个下标的值小于另外一个字母的下标,而后面的下标大于另外一个字母的下标,则也有可能成为回文数。

代码

class Solution {
    public int countSubstrings(String s) {
        int len=s.length();
        char arr[]=s.toCharArray();
        Map <Character,Integer> map=new HashMap();
        int count=0;
        for(int i=0;i<len;i++)
        {
            if(map.get(arr[i])==null)
            {
                map.put(arr[i],i);
            }
            else{
                map.put(i);
            }
        }
        for(char index:map.setKey())
        {
            if(map.get(index).size()>1)
                count+=map.get(index).size()*(map.get(index).size()-1);
            else
                count++;
        }
         return count;
    }
}
class Solution {
    public int countSubstrings(String s) {
        int len=s.length();
        int count=0;
        boolean [][] bool=new boolean [len][len];
        for(int i=0;i<len;i++)
        {
            bool[i][i]=true;
            count++;
        }
        for(int i=len-1;i>=0;i--)
        {
            for(int j=i+1;j<len;j++)
            {
                if(s.charAt(i)==s.charAt(j))
                {
                    if(j-i==1)
                    {
                        bool[i][j]=true;
                    }
                    // if(bool[i+1][j-1]==true)
                    // {
                    //     bool[i][j]=true;
                    // }
                    // else{
                    //     bool[i][j]=false;
                    // }
                    bool[i][j]=bool[i+1][j-1];
                }
                else
                {
                    bool[i][j]=false;
                }
                if(bool[i][j]==true)
                {
                    count++;
                }
            }
        }
        return count;
    }
}
class Solution {
    public int countSubstrings(String s) {
        int len=s.length();
        int count=0;
        boolean [][] bool=new boolean [len][len];
        for(int i=0;i<len;i++)
        {
            bool[i][i]=true;
            count++;
        }
        for(int i=len-1;i>=0;i--)
        {
            for(int j=i+1;j<len;j++)
            {
                if(s.charAt(i)==s.charAt(j))
                {
                    if(j-i==1)
                    {
                        bool[i][j]=true;
                       
                    }
                    // if(bool[i+1][j-1]==true)
                    // {
                    //     bool[i][j]=true;

                    // }
                    // else{
                    //     bool[i][j]=false;
                    // }
                    else
                       bool[i][j]=bool[i+1][j-1];
                }
                else
                {
                    bool[i][j]=false;
                }
                if(bool[i][j]==true)
                {
                    count++;
                }
            }
        }
        return count;
    }
}

结果

 

出来混总是要还的
原文地址:https://www.cnblogs.com/ping2yingshi/p/14335083.html