Question

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.

Solution

动态规划,依次求解长度为1,2,3...的子字符串是否为回文,用一个二维数组纪录,同时用一个计数器统计回文子字符串的个数。

Code

class Solution {
public:
int countSubstrings(string s) {
if (s.length() == 0)
return 0;
int len = s.length();
table.resize(len);
for (int i = 0; i < len; i++)
table[i].resize(len); int count = 0;
for (int i = 0; i < len; i++) {
table[i][i] = true;
count++;
}
// 遍历长度为2,3...的子字符串是否为回文
for (int i = 2; i <= len; i++) {
for (int j = 0; j <= len - i; j++) {
int start = j;
int end = j + i - 1;
if (s[start] == s[end]) {
if (start + 1 < end - 1) {
if (table[start + 1][end - 1] == true) {
table[start][end] = true;
count++;
} else {
table[start][end] = false;
}
} else {
table[start][end] = true;
count++;
}
} else {
table[start][end] = false;
}
}
} return count;
}
vector<vector<bool> > table;
};

LeetCode——Palindromic Substrings的更多相关文章

  1. [LeetCode] Palindromic Substrings 回文子字符串

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

  2. [LeetCode] 647. Palindromic Substrings 回文子字符串

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

  3. LeetCode 647. 回文子串(Palindromic Substrings)

    647. 回文子串 647. Palindromic Substrings 题目描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符 ...

  4. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

  5. 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...

  6. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  7. Leetcode 647. Palindromic Substrings

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

  8. 【Leetcode】647. Palindromic Substrings

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

  9. [Swift]LeetCode647. 回文子串 | Palindromic Substrings

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

随机推荐

  1. android开发时程序出现崩溃问题

    原因:项目目录下的:src与gen中的包名不一致时,出现问题 <manifest xmlns:android="http://schemas.android.com/apk/res/a ...

  2. Netty进行RPC服务器的开发 需要考虑的问题

    谈谈如何使用Netty开发实现高性能的RPC服务器 - Newland - 博客园 http://www.cnblogs.com/jietang/p/5615681.html 如何实现.基于什么原理? ...

  3. FW:主流RPC框架

    主流RPC框架  2015年10月27日  zman  RPC 介绍目前在互联网公司比较流行的开源的RPC框架. RPC框架比较   语言 协议 ​服务治理 ​社区 机构 Hessian 多语言 he ...

  4. python - 安装/解释器/变量

    python的官网: https://www.python.org/ Python环境安装 Windows 安装https://www.python.org/downloads/windows/ Wi ...

  5. ShuffleNet

    ShuffleNet (An Extremely Efficient Convolutional Neural Network for Mobile Devices) —— Face++ shuffl ...

  6. Python(并发编程进程)

    并发编程 二.多进程 要让Python程序实现多进程(multiprocessing),我们先了解操作系统的相关知识. Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊.普通的函 ...

  7. 深入浅出java IO模型

    一.同步和异步 同步:一个事件或者任务的执行,会使整个流程暂时等待,也就是说如果有多个任务要执行,必须要逐个进行. 异步:一个事件或者任务的执行,不会使整个流程暂时等待,也就是说如果有多个任务要执行, ...

  8. nginx 413 request entity too large解决办法

    nginx 出现:413 request entity too large,一般是在上传图片的时候,上传的图片大小超过了服务器设置的最大上传大小,需要修改nginx和PHP的设置: (1)打开 /us ...

  9. Subime3 快捷键

    实用快捷键 Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所有打开文件Ctrl+Shift+ ...

  10. 国内NLP的那些人那些会

    统计学和语言学专家都列在一起了,没有区分.1,黄昌宁,1937年生于广东,1955年考入清华大学电机系,1961年毕业并留校任教至博士生导师, 1983-1984年赴美国耶鲁大学进修,1986-198 ...