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 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.
Approach #1: DFS. [C++] [TEL]
class Solution {
public:
int findSubstringInWraproundString(string p) {
string temp = "zabcdefghijklmnopqrstuvwxy";
for (int i = 0; i < 10; ++i) temp += temp;
set<string> ans;
helper(ans, temp, p, 1);
return ans.size();
}
void helper(set<string>& ans, string& temp, string p, int level) {
if (level > p.length()) return ;
for (int i = 0; i <= p.length() - level; ++i) {
string subString = p.substr(i, level);
int found = temp.find(subString);
if (found != string::npos) {
ans.insert(subString);
//cout << subString << endl;
}
}
helper(ans, temp, p, level+1);
}
};
Approach #2: DP. [Java] [AC]
class Solution {
public int findSubstringInWraproundString(String p) {
int[] count = new int[26];
int curMaxLen = 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))
curMaxLen++;
else
curMaxLen = 1;
int index = p.charAt(i) - 'a';
count[index] = Math.max(count[index], curMaxLen);
}
int sum = 0;
for (int i = 0; i < 26; ++i)
sum += count[i];
return sum;
}
}
Analysis:
The idea is, if we know the max number of unique substrings in p ends with 'a', 'b', 'c' ...... 'z', then the summary of them is the answer.
- 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 they 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 in s since s has infinite length.
- Now we know the max number of unique substrings in p ends with 'a', 'b', ......., 'z' and those substrings are all in s. Summary is the answer, according to the question.
Reference:
https://leetcode.com/problems/unique-substrings-in-wraparound-string/discuss/95439/Concise-Java-solution-using-DP
467. Unique Substrings in Wraparound String的更多相关文章
- 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" ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串
详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 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" ...
- 动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String
2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将 ...
- 467. [leetcode] Unique Substrings in Wraparound String
467. Unique Substrings in Wraparound String Implement atoi to convert a string to an integer. Hint: ...
随机推荐
- ubuntu 安装google输入法
第五步:通常情况下,IBus图标(一个小键盘)会出现在桌面右上角的任务栏中.有时候这个图标会自行消失,可使用以下命令,找回消失的IBus图标: ibus-daemon -drx 不建议用googl ...
- confd test
vi /etc/confd/confd.toml backend = "consul"confdir = "/etc/confd"log-level = &qu ...
- Redis 位操作
[Redis 位操作] 1.GETBIT key offset 对 key 所储存的字符串值,获取指定偏移量上的位(bit). 当 offset 比字符串值的长度大,或者 key 不存在时,返回 0 ...
- PHP逻辑运算符中的and和&&以及or和||是有区别的
下图是PHP的逻辑运算符: 看图中and和&&都是“与”,而or和||都是“或”,初开起来没有区别,但实际上这里面有一个优先级别的区别,即: &&和||的优先级别要高于 ...
- yum 系列(一) yum 和 rpm 常用命令
yum 系列(一) yum 和 rpm 常用命令 一.yum 常用命令 yum 命令:http://man.linuxde.net/yum yum 是在 Fedora 和 RedHat 以及 SUSE ...
- CSS 如何让Table的里面TD全有边框 而Table的右左边框没有
比如这样一个CSS.td3{font-size: 14px;color: #FFFFFF;background-color: #000000;BORDER-RIGHT: #f6f6f6 1px sol ...
- 安装wampserver后,在www文件夹下面写php文件,而在网页里输入localhost而无法打开php文件时解决办法汇总
wampserver安装后,在www文件夹下面写入xx.PHP文件,然后在网页里输入localhost:xx.PHP. 你可能会遇到如下三种情况: 情形一:网页上显示空白,按F12,出现404的错误. ...
- RSA加密解密总结
简单的控制台程序 #include"stdafx.h" #include <math.h> #include<string.h> /*/求解密密钥d的函数( ...
- C++ 的Tool工具收集
C++ 的Tool工具收集 1. muparser - Fast Math Parser Library 数学公式解析函数,开源工具库 网址: http://muparser.beltoforion. ...
- Oracle EBS Standard Package Function Add User & Resp
Oracle EBS Standard Package Function Add User & Resp. fnd_user_pkg.CreateUser; fnd_user_pkg.AddR ...