当前位置:网站首页>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;
}
}
边栏推荐
- Overview of communication protocols
- 好用移动APP自动化测试框架哪里找?收藏这份清单就好了!
- PX4模块设计之十二:High Resolution Timer设计
- 卷积神经网络——24位彩色图像的卷积的详细介绍
- Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing
- 2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ ) type integer in
- Plato Farm全新玩法,套利ePLATO稳获超高收益
- Ribbon load balancing principle and some source codes
- Prometheus node exporter common monitoring indicators
- RN开发系列<9>--Mobx(1)入门篇
猜你喜欢

How to set user-defined display for Jiaming Watch

IP第十四天笔记

第二轮Okaleido Tiger即将登录Binance NFT,或持续创造销售神绩

【机器学习网络】BP神经网络与深度学习-6 深度神经网络(deep neural Networks DNN)

【独立站建设】跨境电商出海开网店,首选这个网站建设!

管理信息系统期末复习

【day02】数据类型转换、运算符、方法入门

微信小程序轮播图
![[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)](/img/f4/70634556c4ae8fc3b087084e1e27b3.png)
[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)

Install and configure Debian on a wired network
随机推荐
Some common instructions in JVM tuning
Overview of communication protocols
ISG index shows that the it and business service market in the Asia Pacific region fell sharply in the second quarter
[day02] Introduction to data type conversion, operators and methods
IIC 通信协议 (一)
F - Pre-order and In-order(Atcoder 255)
可视化领域 SVG
e.target与e.currentTarget的区别
sed输出指定行
数据分析师岗位分析
redux三大核心
Px4 module design 12: high resolution timer design
【机器学习网络】BP神经网络与深度学习-6 深度神经网络(deep neural Networks DNN)
标准C语言11
Differences and uses of SRAM, DRAM, SDRAM and DDR
干货 | 独立站运营怎么提高在线聊天客户服务?
IP第十四天笔记
There are two solutions for the feign call header of microservices to be discarded (with source code)
地平线 旭日X3 PI (四) 板上运行(未写完)
Plato Farm全新玩法,套利ePLATO稳获超高收益