【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 ...
随机推荐
- 【02】json语法
[02] JSON 语法是 JavaScript 语法的子集. JSON 语法规则 JSON 语法是 JavaScript 对象表示法语法的子集. 数据在名称/值对中 数据由逗号分隔 花括号保存对象 ...
- 【Codeforces 1038D】Slime
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 相当于让你确定每个数字前面的系数是-1还是+1 有个结论是这样每次和相邻的减的话, 任何出除了全"-1"和全"+ ...
- 【Codeforces 375B】Maximum Submatrix 2
[链接] 我是链接,点我呀:) [题意] 如果任意行之间可以重新排序. 问你最大的全是1的子矩阵中1的个数 [题解] 设cnt[i][j] 表示(i,j)这个点往右连续的1的个数 我们枚举列j 然后对 ...
- Linear and Logistic Regression in TensorFlow
Linear and Logistic Regression in TensorFlow Graphs and sessions TF Ops: constants, variables, funct ...
- HDU 1249 三角形的分割
可以将三角形的三条边一条一条加进图形中观察 假设添加第n个三角形 前n-1个三角形将区域划分为sum[n-1] 第n个三角形每条边最多能经过前n-1个三角形每条三角形的两条边 , 一条边切完增加了 2 ...
- HDU 1254 条件过程复杂的寻找最短路
这里一看就是找箱子到终点的最短路 一开始还傻傻的以为人的位置给的很没有意思- -,然后果然错了 没过多久想明白了错误,因为你推箱子并不是你想去哪里推就能去哪推的,首先得考虑人能否过的去,因为可能人被箱 ...
- Sliding Window(滑动窗口)
Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 58002 Accepted: 16616 Case Time Limi ...
- kendo Cannot read property 'call' of undefined
造成这个错误的原因是在 必须有id的定义
- 非常适合新手的jq/zepto源码分析01
(function(global, factory) { // 查看这里是不是定义成模块,如果定义模块就返回 一个模块 if (typeof define === 'function' &&a ...
- Open Flash Chart 简介
http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-ofc/ Open Flash Chart(OFC)是一个开源的 Flash 图表绘 ...