动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String...
2018-09-01 22:50:59
问题描述:
问题求解:
如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上。
后来发现是一个动态规划的问题,可以将每个字符结尾的最长长度进行保存,这样就巧妙的解决的重复的问题。
- 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.
public int findSubstringInWraproundString(String p) {int res = 0;int[] dp = new int[26];int maxLen = 0;for (int i = 0; i < p.length(); i++) {if (i > 0 && (p.charAt(i) - p.charAt(i - 1) == 1 || p.charAt(i - 1) - p.charAt(i) == 25)) {maxLen++;}else maxLen = 1;dp[p.charAt(i) - 'a'] = Math.max(dp[p.charAt(i) - 'a'], maxLen);}for (int i = 0; i < 26; i++) res += dp[i];return res;}
转载于:https://www.cnblogs.com/hyserendipity/p/9572041.html
动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String...相关推荐
- [leetcode] 467. Unique Substrings in Wraparound String
Description Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrst ...
- 算法作业系列10——Unique Substrings in Wraparound String
算法作业系列(十) Unique Substrings in Wraparound String 写在前面 如果你是因为没有思路来找答案的,这里建议你去看看题目的s,因为s是一个连续字母的字符串,所以 ...
- Leetcode 467. Unique Substrings in Wraparound String
题目链接:Unique Substrings in Wraparound String Consider the string s to be the infinite wraparound stri ...
- 467. Unique Substrings in Wraparound String
467. Unique Substrings in Wraparound String 题目的意思是给一个无限循环的大字符串s,然后给一个小串p,求出p的子串在s中的个数: 首先可以将其分为26个状态 ...
- [LeetCode]Unique Substrings in Wraparound String@Python
Unique Substrings in Wraparound String Consider the string s to be the infinite wraparound string of ...
- 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 ...
- LeetCode467. Unique Substrings in Wraparound String
题目 Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz&qu ...
- Leetcode-467. Unique Substrings in Wraparound String
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流.因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法.如有错误欢迎指正.博主首发CSDN ...
最新文章
- python需要变量命名规则_和孩子一起学习python之变量命名规则
- Kafka是什么,JMS是什么,常见的类JMS消息服务器,为什么需要消息队列(来自学习笔记)
- 架构设计分布式数据结构与算法面试题
- mysql内置含糊_mySQL入门04 内置函数
- 开发 OpenAM Java 客户端
- LeetCode--80. 删除排序数组中的重复项Ⅱ(双指针,暴力)
- Java学习日报—消息队列—2021/11/23
- MTK 驱动(66)---Android recovery UI实现分析
- Eclipse中 Junit 正常运行完了 可是方法覆盖率全红 解决办法 (附带②EclEmma插件安装方法④覆盖率抽出与合并)
- 把ipad变成电脑的音箱
- Netscreen的岁月 from Sina
- 百度云API怎么使用
- 计算机网络无法连接共享打印机驱动,Windows 10 安装网络共享打印机失败,提示0x00009c4a 无法连接到打印机解决办法...
- 【verbs】ibv_modify_qp()|RDMA
- 用一个div绘制背景流动网格特效
- 单片机中常用的串口通信协议帧
- 前端ECMAScript6个人学习笔记
- 巴贝奇通用计算机设计思想称为,计算机有运算速度快精确度高有记忆和逻辑判断能力.docx...
- python中的pygame弹球游戏代码_python pygame实现挡板弹球游戏
- c#日期格式操作(贴吧转)
热门文章
- 在ArchLinux操作系统安装ctex宏包
- 介绍磁性微球在各领域中的应用
- 一文教会你Flask模板
- 读书笔记_《当下的力量》_精华书摘
- 只要你认真看完一万字☀️Linux操作系统基础知识☀️分分钟钟都吊打面试官《❤️记得收藏❤️》
- jdk8銝要onematch_JDK8新特性详解 - 纯粹而又极致的光--木九天 - OSCHINA - 中文开源技术交流社区...
- LabVIEW实现Modbus通信
- 【微信小程序入门到精通】— 小程序的 API 到底是个啥
- 基于DFA算法实现过滤敏感词
- 当代大学生提升自己的最佳捷径-python