当前位置:网站首页>0 dynamic programming medium leetcode467. The only substring in the surrounding string
0 dynamic programming medium leetcode467. The only substring in the surrounding string
2022-07-27 04:40:00 【18 ARU】
467. The unique substring in the surround string
analysis
Calculate with 26 The length of the longest substring ending with letters , Add up to the number of substrings that can be found in that infinite looping array .
All substrings are based on this 26 Letters ending . Find out respectively with 26 The longest substring ending with letters , You can get all the substrings .
Solution to the problem of the candle with a negative snow :
The local idea is actually 「 The sliding window 」 and 「 Dynamic programming 」 Combination .
Dynamic programming of substring Correlation , The general definition of state is 「 By location ii Ending 、 The length of substring that meets the requirements 」.
Such definition , Mainly to simplify , Because I know dp[i]dp[i] in the future , It's easy to get dp[i + 1]dp[i+1].
For example, for this topic , We define the state as 「pp in , By location ii Ending 、 stay ss The longest substring length in 」.
So in the known dp[i]dp[i] in the future , obtain dp[i + 1]dp[i+1] when , There are only two situations :
When in ss in , p[i + 1]p[i+1] yes p[i]p[i] The next character of , Then it means it can Connected to the , therefore dp[i + 1] = dp[i] + 1dp[i+1]=dp[i]+1.
When in ss in , p[i + 1]p[i+1] No p[i]p[i] The next character of , Then it means it can Can't connect , therefore dp[i + 1] = 1dp[i+1]=1.
author :fuxuemingzhu
link :https://leetcode.cn/problems/unique-substrings-in-wraparound-string/solution/by-fuxuemingzhu-ixas/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
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;
}
}
边栏推荐
- Install and configure Debian on a wired network
- GenericServlet为什么有两个init方法
- timestamp列使用varchar类型和使用date类型有什么区别?
- Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
- 干货 | 独立站运营怎么提高在线聊天客户服务?
- [machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)
- The difference between ArrayList and LinkedList
- 结构型模式-装饰者模式
- 数据库泰斗王珊:努力创新,精心打磨优质的数据库产品
- 详解左值、右值、左值引用以及右值引用
猜你喜欢

ceph操作
![Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量](/img/ed/941276a15d1c4ab67d397fb3286022.png)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量

好用的shell快捷键

Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化

BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明

Understand kingbasees V9 in one picture
![[construction of independent stations] this website is the first choice for cross-border e-commerce companies to open online stores at sea!](/img/26/22597a7ece0431a4e54703945a775c.jpg)
[construction of independent stations] this website is the first choice for cross-border e-commerce companies to open online stores at sea!

Easy to use shell shortcuts

Session&Cookie&token

Prometheus Node Exporter 常用监控指标
随机推荐
Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
F - Pre-order and In-order(Atcoder 255)
【C语言】递归详解汉诺塔问题
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
Why does genericservlet have two init methods
Head detached from origin/... Causes push failure
微信小程序轮播图
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions
JMeter learning notes 004-csv file line number control cycle times
How to set user-defined display for Jiaming Watch
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
Dry goods | how can independent station operation improve online chat customer service?
Nacos startup and login
F - Pre-order and In-order(Atcoder 255)
els 方块显示原理
管理信息系统期末复习
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in
好用的shell快捷键
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual