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. 小程序 Page is not constructed because it is not found.

    如下错误一般发生在点击事件切换页面的时候 解决方式: 在需要切换到的那个页面的js文件中添加Page({ })方法即可解决此问题. Tis:在js文件中输入Page回车,可自动添加Page方法,包括里 ...

  2. MyBatis 工作流程及插件开发

    1. MyBatis 框架分层架构 2. MyBatis 工作流程 获取 SqlSessionFactory 对象: 解析配置文件(全局映射,Sql映射文件)的每一个信息,并保存在Configurat ...

  3. 剑指Offer——和为S的两个数字

    题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的.   输入描述: 对应每个测试案例,输出两个数,小的先输出. ...

  4. lua连接数据库操作示例代码

    lua连接数据库可以使用resty.mysql库 示例代码如下: local mysql = require "resty.mysql" local db, err = mysql ...

  5. 新手怎么读懂一个中型的Django项目

    [前言]中型的项目是比较多的APP,肯会涉及多数据表的操作.如果有人带那就最好了,自己要先了解基本的django框架(MTV ,ORM等)师傅可以给讲解一下框架怎么组织url.py,model.py, ...

  6. error: Error: No resource found for attribute ‘layout_scrollFlags’ in package‘包名’

    遇到error: Error: No resource found for attribute 'layout_scrollFlags' in package'包名' 这个问题时候刚開始自己也是感觉到 ...

  7. Shiro-Base64加密解密,Md5加密

    Shiro权限框架中自带的加密方式有Base64加密,MD5加密 在Maven项目的pom.xml中添加shiro的依赖: <dependency> <groupId>org. ...

  8. Mybatis框架学习总结-解决字段名与实体类属性名不相同的冲突

    在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定是完全相同的. 1.准备演示需要使用的表和数据 CREATE TABLE orders( order_id INT PRIMARY KEY ...

  9. VMware 虚拟机 Ubuntu 不能全屏问题

    在刚安装完ubuntu后,屏幕不能全屏显示,此时: 1.安装VMware Tools 步骤: 1.1     进入ubuntu系统后,点击虚拟机上的[虚拟机]->[安装 vmware tools ...

  10. abap 中modify 的使用

    1.modify table itab from wa Transporting f1 f2 ... 表示表itab中符合工作区wa 中关键字的一条数据的 f1 f2字段会被wa中对应的字段值更新. ...