Leetcode: Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". Now we have another string p. Your job is to find out how many unique non-empty substrings of p are present in s. In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s. Note: p consists of only lowercase English letters and the size of p might be over 10000. Example 1:
Input: "a"
Output: 1 Explanation: Only the substring "a" of string "a" is in the string s.
Example 2:
Input: "cac"
Output: 2
Explanation: There are two substrings "a", "c" of string "cac" in the string s.
Example 3:
Input: "zab"
Output: 6
Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.
这道题我一开始发现了数学规律:
length of consecutive chars vs number of different substring
“a” -> 1 = 1
"ab" -> 3 = 1+2
"abc" -> 6 = 1+2+3
"abcd" -> 10 = 1+2+3+4
于是打算计算string p里面每一段consecutive substring的length是多少,比如abcxyz, 两段分别为“abc”“xyz”, len分别为3和3,#of substring分别是6和6,总数是6+6。 但是这个方法没法处理有重复的,比如abczab,"abc"依然能产生6个substring, 然而"zab"就有重复了,只能产生3个different substring.
如何处理重复呢? “abc”和“zab”里面的"ab"要同时考虑到,于是就有了DP的想法:
total number of different substring = sum of {number of different substring end by each char}
Further,
number of different substring end by each char = length of longest contiguous substring ends with that letter.
Example "abcd", the 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.
public class Solution {
public int findSubstringInWraproundString(String p) {
if (p==null || p.length()==0) return 0;
int[] nums = new int[26]; //numbers of different substrings end with a specific char
int longestConsec = 0;
for (int i=0; i<p.length(); i++) {
if (i>0 && (p.charAt(i-1)+1 == p.charAt(i) || p.charAt(i-1)-25 == p.charAt(i))) {
longestConsec++;
}
else longestConsec = 1;
char c = p.charAt(i);
nums[(int)(c-'a')] = Math.max(nums[(int)(c-'a')], longestConsec);
}
int res = 0;
for (int num : nums) {
res += num;
}
return res;
}
}
Leetcode: Unique Substrings in Wraparound String的更多相关文章
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 467. [leetcode] Unique Substrings in Wraparound String
467. Unique Substrings in Wraparound String Implement atoi to convert a string to an integer. Hint: ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- 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" ...
- [Swift]LeetCode467. 环绕字符串中唯一的子字符串 | 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" ...
- 动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String
2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将 ...
- 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串
详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...
随机推荐
- DDD~概念中的DDD(转)
概念中的DDD DDD: 领域驱动设计,它是对面向对象的的分析和设计(OOAD,Object Orient Analysis Design)的一个补充,对技术框架进行了分层规划,同时对每个类进行了策略 ...
- 【DP】HDU 1176
HDU 1176 免费馅饼 题意:中文题目不解释. 思路:因为是从中间出发所以思路卡了许久,还在之前做了道HIHO入门的题.能想到的点,从时间思考,然后初始化1s的时候,4,5,6,的数值要特别赋值. ...
- 【DP】POJ 2385
题意:又是Bessie 这头牛在折腾,这回他喜欢吃苹果,于是在两棵苹果树下等着接苹果,但苹果不能落地后再接,吃的时间不算,假设他能拿得下所有苹果,但是这头牛太懒了[POJ另一道题目说它是头勤奋的奶牛, ...
- python字符串
字符串格式化 字符串格式化使用字符串格式化操作符%来实现:格式化字符串 % 值(字符串或者数字或者多个值的元组,字典) >>> format = "hello, %s. % ...
- 解决Windows Server2008R2中导入Excel不能使用Jet 4.0
操作系统使用了Windows Server2008R2(Windows Server2008R2是64位系统,同时配置IIS7.0 原读取Excel文件代码为: string strConn = st ...
- 键盘和alertView的冲突问题
冲突现象: textField失去响应收键盘时,如果要弹出一个alertView警告框,就会出现一个现象:在点击了alertView上的事件后,在alertView消失后,会有短暂的出现键盘会再次弹出 ...
- Oracel EBS - Search Report by Response & Group
- 如何激活win10 win10激活工具下载
http://www.2cto.com/os/201511/448815.html 官方的win10出来了,可是装在上电脑后要花钱才能用,费用要好几百呢,感觉很不值得,这里我教给大家个免费激活官方wi ...
- sun公司的jstl标签库
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- [转载] Win PE内安装Windows 7原版镜像 / 安装程序无法创建新的系统分区,也无法定位现有的系统分区
格式化C盘为NTFS格式 解压ISO安装文件中找到BOOT.BOOTMGR和SOURCES这三个文件到C盘根目录下:或者复制BOOT.BOOTMGR,在C盘新建文件夹SOURCES,复制ISO安装文件 ...