# LeetCode-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:

1. The input string length won't exceed 1000.

输入: "abc"

输入: "aaa"



1. 输入的字符串长度不超过1000。

C++代码（Visual Studio 2017):

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Solution {
public:
void count(string s, int i, int j, int& res) {
while (i >= 0 && j < s.size() && s[i] == s[j]){
i--; j++; res++;
}
}

int countSubstrings(string s) {
if (s.empty())
return 0;
int res=0;
for (int i = 0; i < s.size(); i++) {
count(s, i, i, res);
count(s, i, i + 1, res);
}
return res;
}
};

int main()
{
Solution s;
string str = "aaa";
int result = s.countSubstrings(str);
cout << result << endl;
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120