leetcode 467. Unique Substrings in Wraparound String | 467. 环绕字符串中唯一的子字符串(动态规划)
题目
https://leetcode.com/problems/unique-substrings-in-wraparound-string/
题解
1、dp 超时版本
class Solution {public int findSubstringInWraproundString(String p) {HashSet<Integer>[] arr = new HashSet[26]; // <结尾字母,长度>for (int i = 0; i < 26; i++) {arr[i] = new HashSet();}arr[p.charAt(0) - 'a'].add(1);int curLen = 1; // 避免误将断链情况计入结果for (int i = 1; i < p.length(); i++) {arr[p.charAt(i) - 'a'].add(1);if (p.charAt(i) == 'a' && p.charAt(i - 1) == 'z') {curLen++;for (int len : arr[25]) {if (len + 1 <= curLen) arr[0].add(len + 1);}} else if (p.charAt(i - 1) == p.charAt(i) - 1) {curLen++;for (int len : arr[p.charAt(i - 1) - 'a']) {if (len + 1 <= curLen) arr[p.charAt(i) - 'a'].add(len + 1);}} else {curLen = 1;}}int result = 0;for (int i = 0; i < 26; i++) {result += arr[i].size();}return result;}
}
2、dp 答案版本
参考:Concise Java solution using DP
After failed with pure math solution and time out with DFS solution, I finally realized that this is a DP problem…
The idea is, if we know the max number of unique substrings in p
ends with 'a', 'b', ..., 'z'
, then the summary of them is the answer. Why is that?
- The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that letter. Example
"abcd"
, the max number of unique substring ends with'd'
is 4, apparently they are"abcd", "bcd", "cd" and "d"
. - If there are overlapping, we only need to consider the longest one because it covers all the possible substrings. Example:
"abcdbcd"
, the max number of unique substring ends with'd'
is 4 and all substrings formed by the 2nd"bcd"
part are covered in the 4 substrings already. - No matter how long is a contiguous substring in
p
, it is ins
sinces
has infinite length. - Now we know the max number of unique substrings in
p
ends with'a', 'b', ..., 'z'
and those substrings are all ins
. Summary is the answer, according to the question.
Hope I made myself clear…
class Solution {public int findSubstringInWraproundString(String p) {int[] dp = new int[26]; // <结尾字母,以当前字母结尾的最长串长度>dp[p.charAt(0) - 'a'] = 1;int curLen = 1;for (int i = 1; i < p.length(); i++) {if (p.charAt(i - 1) == p.charAt(i) - 1 || p.charAt(i) == 'a' && p.charAt(i - 1) == 'z') {curLen++;} else {curLen = 1;}dp[p.charAt(i) - 'a'] = Math.max(dp[p.charAt(i) - 'a'], curLen);}int result = 0;for (int i = 0; i < 26; i++) {result += dp[i];}return result;}
}
leetcode 467. Unique Substrings in Wraparound String | 467. 环绕字符串中唯一的子字符串(动态规划)相关推荐
- 467. Unique Substrings in Wraparound String
467. Unique Substrings in Wraparound String 题目的意思是给一个无限循环的大字符串s,然后给一个小串p,求出p的子串在s中的个数: 首先可以将其分为26个状态 ...
- 【宫水三叶的刷题日记】467. 环绕字符串中唯一的子字符串(中等)
题目描述 这是 LeetCode 上的 467. 环绕字符串中唯一的子字符串 ,难度为 中等. Tag : 「线性 DP」.「树状数组」 把字符串 s 看作是 "abcdefghijklmn ...
- [leetcode] 467. Unique Substrings in Wraparound String
Description Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrst ...
- Leetcode 467. Unique Substrings in Wraparound String
题目链接:Unique Substrings in Wraparound String Consider the string s to be the infinite wraparound stri ...
- leetcode - 467. 环绕字符串中唯一的子字符串
解题思路:dp[i]表示以第i个字母结尾的最长的子字符串的长度.那么字符串P的不同子字符串的数量为dp[0] + dp[1] + - + dp[25] (从a一直加到z).循环P中的每一个字符,不断更 ...
- leetcode - 467. Unique Substrings in Wraparound String
算法系列博客之Dynamic Programming 本篇博客将运用动态规划的思想来解决leetcode上467号问题 问题描述: Consider the string s to be the in ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- leetcode算法题--环绕字符串中唯一的子字符串★
原题链接:https://leetcode-cn.com/problems/unique-substrings-in-wraparound-string/ 思路是dp数组保存以26个字母为结尾的子字符 ...
- LeetCode 467. 环绕字符串中唯一的子字符串(思维转换)
1. 题目 把字符串 s 看作是"abcdefghijklmnopqrstuvwxyz"的无限环绕字符串,所以 s 看起来是这样的:"-zabcdefghijklmnop ...
最新文章
- java myeclipse 下载_myeclipse 10|MyEclipse(优秀的Java开发工具myeclipse下载) 10.7官方版下载 - 下载吧...
- linux 日志按大小切割_nginx入门详解(六)- 日志切割
- java 10新_【Java基础】Java10 新特性
- 仿射密码 [GKCTF2020]小学生的密码学
- 页面某个模块的文字内容是动态的,可能是几个字,也可能是一句话。然 后,希望文字少的时候居中显示,文字超过一行的时候居左显示。该如何实现?...
- h5页面如何预览excel文件_移动端页面,如何解析预览 word/excel/PDF文件?
- MongoDB 插入、更新、删除
- Linux下的hostname命令
- 【恋上数据结构】图代码实现、最小生成树(Prim、Kruskal)、最短路径(Dijkstra、Bellman-Ford、Floyd)
- pytorch中mask操作之torch.masked_select
- linux 邮件文件名 病毒,文件型File/Macro病毒的捕获
- python监控窗口_Windows下python监控脚本
- 用word怎么做c语言框图作业,在Word中绘制简单C语言流程图(精)
- LU分解法求解线性方程组
- 如何恢复Windows默认共享
- VTK四面体文件格式
- excel表格横向纵向变换_excel2016如何把纵向的数据变为横向
- 证券行业的数字化转型:数字化企业的特征和创新案例
- 为 a.out 举行一个特殊的告别仪式
- Linux高性能服务器编程——书籍阅读笔记
热门文章
- CodeForces - 1092F Tree with Maximum Cost(树形dp+树根转移)
- 卡在linuxctrld进系统_Linux系统卡死后紧急处理
- hdu3007(最小覆盖圆问题)
- SBT模版(Size Balanced Tree)
- Lua table(表)
- 非阻塞模式WinSock编程入门
- OkHttp3 HTTP请求执行流程分析
- linux问题排查常用命令详解
- IDEA中常用快捷键整理及重置快捷键
- JDBC连接失败java.sql.SQLException: ...ClassCastException: BigInteger cannot be cast to Long