当前位置:网站首页>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;
}
}
边栏推荐
- grid布局
- Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
- 第4章 Bean对象的作用域以及生命周期
- Dino paper accuracy, and analyze the variant of its model structure & Detr
- 【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)
- 从零开始C语言精讲篇4:数组
- Pinia uses the whole process, including state, actions, getters, and how to deconstruct, respond, and various methods used by actions
- Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
- How to set user-defined display for Jiaming Watch
- Px4 module design 12: high resolution timer design
猜你喜欢

结构型模式-装饰者模式

第六章:云数据库

【C语言】递归详解汉诺塔问题
![[C language] recursively explain the tower of Hanoi problem](/img/a6/bbf1f19fc2a663df155cca53f2538a.png)
[C language] recursively explain the tower of Hanoi problem

可视化领域 SVG

Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon

Head detached from origin/... Causes push failure

JS three methods of traversing arrays: map, foreach, filter

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

Final review of management information system
随机推荐
匿名命名管道, 共享内存的进程间通信理解与使用
Ref Hook
Prometheus node exporter common monitoring indicators
[day02] Introduction to data type conversion, operators and methods
ROS camera calibration sensor_ Msgs/camerainfo message data type and meaning
利用JSON类型在mysql中实现数组功能
详解左值、右值、左值引用以及右值引用
可视化领域 SVG
Effect Hook
Chapter 6: cloud database
Head detached from origin/... Causes push failure
sed输出指定行
F - Pre-order and In-order(Atcoder 255)
Word/excel has a fixed table size. When filling in the content, the table does not change with the cell content
一张图看懂KingbaseES V9
Interview must ask | what stages does a thread go through from creation to extinction?
Why does genericservlet have two init methods
The data in echart histogram is displayed at the top of the chart
P1438 无聊的数列 线段树+差分
Playwright web crawler actual battle case sharing