【LeetCode】467. 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.
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.
This is the third problem in weekly contest 11. It's true that there are many brilliant pipo. Hope next time is me.
Causing there are too many common substrs at the end of any characters among 'a' - 'z'. How can I count them without repeating.
A simply solution is calculating every sub-string in p and finding out if it is a common sub-string with s. But its too expensive in time which is O(n*n*n).
The brilliant ideal is using one loop to travel p and using a variable "pos" to record the mis-match position, thus we can get the maximum length of common string ending at every character('a' - 'z')。Finally,we count the number of common sub-string by the length (num = length). For example, "abc"'s max common substring len is 3. Common substrings are "abc" "bc" "c" .
Here comes the code:
class Solution {
public:
int findSubstringInWraproundString(string p) {
int lenp = p.length(), cnt = , pos = ;
if(lenp <= ) return ;
vector<int>length(, ); /*length['a']:用来记录以'a'作为结尾的公共子串的最大长度 (因为结尾固定,按照相同的规律发展,长度越长那么细分出来的公共子串数目 = length 越多) */
p += '#';
lenp = p.length();
for(int i = ; i < lenp; i ++){
if((p[i] - 'a') % != (p[i - ] - 'a' + ) % ){
for(int j = pos; j < i; j ++)
length[p[j]] = max(length[p[j]], j - pos + );
pos = i;
}
}
for(int c = 'a'; c <= 'z'; c ++){
cnt += length[c];
}
return cnt;
}
};
Do not waste any second in your life. Be strong , be confident.
【LeetCode】467. Unique Substrings in Wraparound String的更多相关文章
- 【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" ...
- 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 ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
随机推荐
- spring与quartz整合实现分布式动态创建,删除,改变执行时间定时任务(mysql数据库)
背景:因为在项目中用到了定时任务,当时想到了spring的quartz,写完发现费了很大功夫,光是整合就花了一上午,其中最大的问题就是版本问题,项目中用的是spring3.2.8的版本,查阅发现,3. ...
- 抽象类和接口有什么区别---https://blog.csdn.net/csdn_aiyang/article/details/71171886
https://blog.csdn.net/csdn_aiyang/article/details/71171886 概念] 抽象类.具体类是相对的,并非绝对的.抽象是一种概念性名词,具体是一种可 ...
- noip模拟赛 柜(暴力)
分析:暴力的方法是非常显然的,从起点走一次,从终点走一次,路径相交的点即为所求,但是这样存图都很难存下,而且如果数据极端可能要求R*C次,时间空间都受不了.如果不需要记录整张图,并且一次能移动很多步就 ...
- 转载 - JSONObject简介
出处: http://www.cnblogs.com/java-pan/archive/2012/04/07/JSONObject.html JSONObject简介 本节摘要:之前对JSON做了 ...
- MyBatis3错误:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Priority的问题解决
在使用Maven新建QuitStart类型项目时,引入了MyBatis3.2.0版本的JAR包之后,出现如下错误: Exception in thread "main" java. ...
- Html5离线缓存简介
一. 什么是manifest 首先manifest是一个后缀名为minifest的文件,在文件中定义那些需要缓存的文件,支持manifest的浏览器,会将按照manifest文件的规则,像文件保存在本 ...
- jquery simple modal
窗体API定义丰富,而且使用也很容易上手.官方地址:http://www.ericmmartin.com/projects/simplemodal/从官方下载插件,在文件中引用<script t ...
- PHP array_merge()
定义和用法 array_merge() 函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的).如果数组是数字索引的,则键名会以连续方式重新索引. ...
- I/O虚拟化
note:这里主要记录我对IO虚拟化的理解,希望这篇文章对想了解虚拟化IO的同学有点帮助.这是我在看论文[vale,a switched ethernet for virtual machines]的 ...
- poj-1635 Subway tree systems(推断两个有根树是否同构)-哈希法
Description Some major cities have subway systems in the form of a tree, i.e. between any pair of st ...