当前位置:网站首页>力扣解法汇总467-环绕字符串中唯一的子字符串
力扣解法汇总467-环绕字符串中唯一的子字符串
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:
描述:
把字符串 s 看作是 “abcdefghijklmnopqrstuvwxyz” 的无限环绕字符串,所以 s 看起来是这样的:
"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...." .
现在给定另一个字符串 p 。返回 s 中 唯一 的 p 的 非空子串 的数量 。
示例 1:
输入: p = "a"
输出: 1
解释: 字符串 s 中只有一个"a"子字符。
示例 2:
输入: p = "cac"
输出: 2
解释: 字符串 s 中的字符串“cac”只有两个子串“a”、“c”。.
示例 3:
输入: p = "zab"
输出: 6
解释: 在字符串 s 中有六个子串“z”、“a”、“b”、“za”、“ab”、“zab”。
提示:
1 <= p.length <= 105
p 由小写英文字母构成
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/unique-substrings-in-wraparound-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 核心就是求以某个字母开头的字符串,最长长度有多少。短的那个一定是长的子串。
代码:
public class Solution467 {
public int findSubstringInWraproundString(String p) {
int[] dp = new int[26];
int k = 0;
for (int i = 0; i < p.length(); ++i) {
if (i > 0 && (p.charAt(i) - p.charAt(i - 1) + 26) % 26 == 1) { // 字符之差为 1 或 -25
++k;
} else {
k = 1;
}
dp[p.charAt(i) - 'a'] = Math.max(dp[p.charAt(i) - 'a'], k);
}
return Arrays.stream(dp).sum();
}
}边栏推荐
- Metaverse × How will smart cities develop?
- Basic use of MATLAB
- On the night of the joint commissioning, I beat up my colleagues
- 颠倒字符串中的单词(split、双端队列)
- serialization and deserialization
- Pyinstaller packaging Exe (detailed tutorial)
- C language programming classic games - minesweeping
- Google Ads 的优势
- The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries
- 国资入股,建业地产这回稳了吗?
猜你喜欢

Redis实现消息队列的4种方案

竞价广告每次点击出价多少钱是固定的吗?

Alicloud OSS file upload system

ozzanimation-基於sse的動作系統

redis集群(cluster)+哨兵模式+主从(replicas)

Ozzanation - système d'action basé sur sse

Explore performance optimization! Performance improvement from 2 months to 4 hours!

How to automatically color cells in Excel

In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!

UE4\UE5触摸屏touch事件:单指、双指
随机推荐
Dataset how to use dataset gracefully. After reading this article, you can fully understand the dataset in c7n/choerodon/ toothfish UI
Leetcode 45 jump game II
Advantages of Google ads
Linux(CentOS6)安装MySQL5.5版本数据库
ozzanimation-基于sse的动作系统
“中国东信杯”广西大学第四届程序设计竞赛(同步赛)
入手Ticwatch2
What are the preparations for setting up Google search advertising series?
[learn FPGA programming from scratch -19]: quick start chapter - operation steps 4-1- Verilog software download and construction of development environment - Altera quartz II version
PHP builds a high-performance API architecture based on sw-x framework (III)
LeetCode LCP 07. Deliver information
LeetCode LCP 07. 传递信息
How can low code platforms improve cost effectiveness?
Make ads more relevant by searching for additional information about ads
How to stop anti-virus software from blocking a web page? Take gdata as an example
如何让杀毒软件停止屏蔽某个网页?以GDATA为例
Huawei intermodal game or application review rejected: the application detected payment servicecatalog:x6
Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
What insurance products can you buy at the age of 60?
Leetcode 55 jump game