当前位置:网站首页>0动态规划中等 LeetCode467. 环绕字符串中唯一的子字符串
0动态规划中等 LeetCode467. 环绕字符串中唯一的子字符串
2022-07-27 04:18:00 【18阿鲁】
分析
计算出分别以26个字母为结尾的最长子字符串的长度,加起来就是所有能够在那个无限循环的数组的找到的子字符串的个数。
所有的子串都是以这26个字母为结尾的。分别求出以26个字母为结尾的最长子字符串,就能得到全部的子字符串。
负雪明烛的题解:
本地的思想其实是「滑动窗口」和「动态规划」相结合。
子串相关的动态规划,一般状态的定义都是「以位置 ii 作为结尾的、符合要求的子串长度」。
这样定义,主要是为了简化,因为知道了 dp[i]dp[i]以后,很容易就能得到 dp[i + 1]dp[i+1]。
比如对于本题,我们把状态定义为「pp 中,以位置 ii 作为结尾的、在 ss 中存在的最长子串长度」。
那么在已知了 dp[i]dp[i] 以后,得到 dp[i + 1]dp[i+1] 时,无非两种情况:
当在 ss中, p[i + 1]p[i+1] 是 p[i]p[i] 的下一个字符,那么说明能 连接上,于是 dp[i + 1] = dp[i] + 1dp[i+1]=dp[i]+1。
当在 ss中, p[i + 1]p[i+1] 不是 p[i]p[i] 的下一个字符,那么说明能 连接不上,于是 dp[i + 1] = 1dp[i+1]=1。
作者:fuxuemingzhu
链接:https://leetcode.cn/problems/unique-substrings-in-wraparound-string/solution/by-fuxuemingzhu-ixas/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public int findSubstringInWraproundString(String p) {
char[] arr = p.toCharArray();
int[] dp = new int[26];
int len = 1;
dp[arr[0]-'a'] = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] - arr[i-1] == 1 || arr[i] == 'a' && arr[i-1] == 'z') {
len++;
} else {
len = 1;
}
if (len > dp[arr[i]-'a']) {
dp[arr[i]-'a'] = len;
}
}
int ans = 0;
for (int num : dp) {
ans += num;
}
return ans;
}
}
边栏推荐
- 项目参数做成可配置项,@ConfigurationProperties注解的使用
- Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
- 2022杭电多校联赛第三场 题解
- Do you know about wechat merchant billing?
- STM32基于HAL库的串口接受中断和空闲中断
- Spark practice case (upgraded version)
- 管理信息系统期末复习
- sed输出指定行
- Okaleido ecological core equity Oka, all in fusion mining mode
- 哈希表刷题(下)
猜你喜欢

BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions

redux三大核心

playwright网络爬虫实战案例分享

Brightcove appoints Dan Freund as chief revenue Officer

数据库泰斗王珊:努力创新,精心打磨优质的数据库产品

Practice of microservice in solving Library Download business problems

安全第四次课后练习

Echart柱状图中数据显示在图上方

Deep analysis - dynamic memory management

Final review of management information system
随机推荐
Some common instructions in JVM tuning
Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing
els_ Rectangle drawing, code planning and backup
卷积神经网络——24位彩色图像的卷积的详细介绍
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products
STM32基于HAL库的串口接受中断和空闲中断
How to set user-defined display for Jiaming Watch
Convolution neural network -- convolution of gray image
ELS square display principle
[leetcode] day104 no overlapping interval
Session&Cookie&token
Remember the major performance problems caused by a TCP packet loss
Chapter 6: cloud database
Okaleido ecological core equity Oka, all in fusion mining mode
People don't talk much, engineers don't talk much
Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon
[small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation
【day02】数据类型转换、运算符、方法入门
[day02] Introduction to data type conversion, operators and methods