当前位置:网站首页>【leetcode】647. Palindrome substring
【leetcode】647. Palindrome substring
2022-04-21 11:00:00 【Front end corner】
【leetcode】647. Palindrome string
subject
Give you a string s , Please count and return this string Palindrome string Number of .
Palindrome string Is reading the same string as reading it backwards .
Substring Is a sequence of consecutive characters in a string .
A substring with different start or end positions , Even if it's made up of the same characters , It's also seen as a different substring .
Example 1:
Input :s = “abc”
Output :3
explain : Three palindrome strings : “a”, “b”, “c”
Example 2:
Input :s = “aaa”
Output :6
explain :6 Palindrome substrings : “a”, “a”, “a”, “aa”, “aa”, “aaa”
Tips :
1 <= s.length <= 1000
s It's made up of lowercase letters
Ideas

Train of thought : double for Circular violence
Train of thought two : Dynamic programming
- set up
dp[i][j]Indicates the interval[i,j]( Left and right closed ) Whether the substring of is a palindrome string , Yes, it is true, Otherwise false - Recursive relations , It is divided into 2 In this case
- Situation 1 :
s[i] !== s[j], Then interval[i,j]The substring of must not be a palindrome string ,dp[i][j] = false - Situation two :
s[i] === s[j], At this time, it is divided into 3 Consider two situations- When
i === j, such as'a'This is a palindrome string ,dp[i][j] = true - When
j - i === 1, such asaaThis also belongs to palindrome string ,dp[i][j] = true - When
j - i > 1, Then it depends on[i+1 ,j-1]Whether it is palindrome string , Whendp[i+1][j-1] === truewhen ,dp[i][j] = true; Whendp[i+1][j-1] === falsewhen ,dp[i][j] = false
- When
- Situation 1 :
- dp The array is all initialized to false
- From the third case in case 2 , Need basis
dp[i+1][j-1]To get the value ofdp[i][j]Value , anddp[i+1][j-1]staydp[i][j]The lower left corner of the , So it lasts from left to right , From bottom to top .
Code

Train of thought : violence
/** * @param {string} s * @return {number} */
var countSubstrings = function(s) {
function isPalindrome(s , start , end){
let str = s.slice(start , end + 1)
return str === str.split('').reverse().join('')
}
let ans = 0
for(let i = 0 ; i < s.length ; i++){
for(let j = i ; j < s.length ; j++){
if(isPalindrome(s , i , j)) ans++
}
}
return ans
};
Train of thought two : Dynamic programming
/** * @param {string} s * @return {number} */
var countSubstrings = function(s) {
let len = s.length
let ans = 0
let dp = (new Array(len)).fill(false).map(x => (new Array(len)).fill(false))
for(let i = len - 1 ; i >= 0 ; i--){
for(let j = i ; j < len ; j++){
if(s[i] === s[j]){
if(j - i <= 1 || dp[i+1][j-1]){
dp[i][j] = true
ans++
}else{
dp[i][j] = false
}
}
}
}
return ans
};
Complexity
-
Time complexity
- Train of thought : O ( n 3 ) O(n^3) O(n3)
- Train of thought two : O ( n 2 ) O(n^2) O(n2)
-
Spatial complexity
- Train of thought : O ( n ) O(n) O(n)
- Train of thought two : O ( n 2 ) O(n^2) O(n2)
Pay attention to my special column , Update three times a day leetcode Answer key , Get stronger together !
版权声明
本文为[Front end corner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211054292836.html
边栏推荐
- Special training of AC automata
- 桶排序 ← C语言实现
- RMQ & segment tree review
- An error occurred while processing your request... enable the Development environment by setting ...
- AcWing 1725. 组队井字游戏(枚举)
- 省赛练习2——第八届福建省大学生程序设计竞赛 &补题
- There was a problem during PIP install, unable to connect (PIP configure mirror source)
- 后缀数组应用
- 8-channel can FD, more powerful data recorder gl3400
- Go language error handling
猜你喜欢

你的思维会改变你的行为,你的行为会改变你的境遇

【面试普通人VS高手系列】b树和b+树的理解

【leetcode】647.回文子串

【生活中的逻辑谬误】对人不对事和两难陷阱

Activity registration | how to quickly complete the development of data sources and targets based on the open source project tapdata PDK?

手把手教你:基于深度学习的滚动轴承故障诊断

AcWing 1761. 阻挡广告牌 (计算几何,两个矩形的交集)

Suffix array application

从思维转变看数字化转型 IT 经营

TypeError: The view function did not return a valid response. The function either returned none
随机推荐
便利店卷疯了:便利蜂、罗森、易捷“激战”
后缀数组模版代码解析
AC自动机&后缀数组复习
Where is London gold safe to open an account?
@Lookup
电机控制-速度环设计
pgpool-II 4.3 中文手册 - 入门教程
后缀数组
手把手教你在云上开发一个图片压缩工具
js---call,apply,bind
Question brushing & Competition & Review
Suffix array special training
犀牛软件插件-rhino插件-visual studio-创建你的第一个插件
MATLAB GUI应用---摇奖(动画演示)
Redis数据库
Go语言错误处理
Activity registration | how to quickly complete the development of data sources and targets based on the open source project tapdata PDK?
First go program
IoT平台如何实现业务配置中心
依然AC自动机