动态规划-独特的子字符串存在于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 inssinceshas infinite length.- Now we know the max number of unique substrings in
pends 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;
}
动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String的更多相关文章
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- [Swift]LeetCode467. 环绕字符串中唯一的子字符串 | Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- Leetcode: Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- LeetCode 467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 【LeetCode】467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串
详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...
- 467. [leetcode] Unique Substrings in Wraparound String
467. Unique Substrings in Wraparound String Implement atoi to convert a string to an integer. Hint: ...
随机推荐
- linux下网卡bonding配置(转)
linux下网卡bonding配置 章节 bonding技术 centos7配置bonding centos6配置bonding 一.bonding技术 bonding(绑定)是一种linux系统 ...
- oracle_sqlplus命令行乱码问题解决
在linux以及unix中,sqlplus的上下左右.回退无法使用,会出现乱码情况. 而rlwrap这个软件就是用来解决这个的. 首先下载rlwrap包:https://linux.linuxidc. ...
- 20180307-Xen、KVM、VMware、hyper-v等虚拟化技术的比较
xen和kvm,是开源免费的虚拟化软件. vmware是付费的虚拟化软件. hyper-v比较特别,是微软windows 2008 R2附带的虚拟化组件,如果你买了足够的授权,hyper-v(包括hy ...
- 20145212 罗天晨 WEB登陆发贴及会话管理功能的实现
会话管理简介 Cookie: cookie常用于识别用户. cookie 是服务器留在用户计算机中的小文件,每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie. 通过PHP能够创建并取回 ...
- Ubuntu 18.04.1更改屏幕分辨率
- undefined reference to `vtable for MyColor'
MyColor是新建的类,原因是使用了QObject,但是系统没有反应过来 解决:从工程删除,再添加进去[QtCreator]
- java中读取配置文件的方法
转自:http://blog.csdn.net/stypace/article/details/38414871 一.使用org.apache.commons.configuration 需要使用的是 ...
- Python3 tkinter基础 Label pack 设置控件在窗体中的位置
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 关于BOARD_SYSTEMIMAGE_PARTITION_SIZE【转】
本文转载自:https://blog.csdn.net/ttxgz/article/details/7542380 1. 系统需要,把需要预置在系统的所有apk放在目录 device/softwinn ...
- Markdon 作图语法 CSDN
插入甘特图 gantt dateFormat YYYY-MM-DD title Adding GANTT diagram functionality to mermaid section 现有任务 已 ...