当前位置:网站首页>LeetCode:647. 回文子串
LeetCode:647. 回文子串
2022-07-30 06:14:00 【Bertil】
给你一个字符串 s ,请你统计并返回这个字符串中 回文子串 的数目。
回文字符串 是正着读和倒过来读一样的字符串。
子字符串 是字符串中的由连续字符组成的一个序列。
具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。
示例 1:
输入:s = "abc"
输出:3
解释:三个回文子串: "a", "b", "c"
示例 2:
输入:s = "aaa"
输出:6
解释:6个回文子串: "a", "a", "a", "aa", "aa", "aaa"
提示:
- 1 <= s.length <= 1000
- s 由小写英文字母组成
### 解题思路 1.首先定义一个变量count来计算回文子串的个数 2.然后使用双指针遍历出所有的子串(分别按顺序和逆序累加字符串),然后判断两个子串是否相等,若相等(即为回文子串)则count++ 3.最后返回count即可
代码
/** * @param {string} s * @return {number} */
var countSubstrings = function(s) {
let count = 0;
for (let i = 0; i < s.length; i++) {
let s1 = '', s2 = '';
for (let j = i; j < s.length; j++) {
s1 = s1 + s[j], s2 = s[j] + s2;// 分别按顺序和逆序累加字符串
if (s1 === s2) count++;
}
}
return count;
};
边栏推荐
猜你喜欢
![[硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!](/img/9a/f3e4bdd0ce8ec153a8e6bdbff5647e.jpg)
[硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!

bean的生命周期

selenium模块

How to calculate the daily cumulative capital flow one by one in real time

C# uses RestSharp to implement Get, Post requests (2)

Graphical relational database design ideas, this is too vivid

谷粒商城--环境部署(2022/7/28最新)

DNS domain name resolution services

RAID disk array

What new materials are used in the large aircraft C919?
随机推荐
golang : Zap日志整合
Go 使用 freecache 缓存
Go: go - redis based operation
谷粒商城--环境部署(2022/7/28最新)
Go 结合Gin导出Mysql数据到Excel表格
C language custom types, rounding
物联网网关该怎么选
限塑令下的新材料——聚乳酸(PLA)
Keil编译大小和存储说明
适合程序员的输入法
A magical no main method of code
解决datagrip连接sqlserver报错:[08S01] 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。
DP5340国产替代CM5340立体声音频A/D转换器芯片
首届人工智能安全大赛正式启动
专访蚂蚁:这群技术排头兵,如何做好底层开发这件事?| 卓越技术团队访谈录
理解和熟悉递归中的尝试
bean的生命周期
k8s 部署mysql8(PV和PVC 版本)
goto语句
Goto statements