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: ...
随机推荐
- ADO Connection failure
使用ado连接,频繁报错.为何?是网络的问题吗?太灵敏了.Connection failure. 跟大文本又关系??
- JAVA_02
class Test2_Extents{ public static void main(String[] args){ System.out.println("Hello World&qu ...
- zookeeper分布式锁和服务优化配置
转自:https://www.jianshu.com/p/02eeaee4357f?utm_campaign=maleskine&utm_content=note&utm_medium ...
- cp & scp
[cp & scp] Linux为我们提供了两个用于文件copy的命令,一个是cp,一个是scp,但是他们略有不同. cp --- 主要是用于在同一台电脑上,在不同的目录之间来回copy文 ...
- Division of Line Segment
Division of Line Segment /** */ void Line::EqualDivision(int nCount, QLineF fline, QList<QPointF& ...
- MVC知识记录
1.完整深入分析 MVC请求机制:http://blog.jobbole.com/85033/ 2.MVC入门:http://www.aizhengli.com/givecase-aspnet-5-m ...
- Linux addr2line命令
一.简介 Addr2line (它是标准的 GNU Binutils 中的一部分)是一个可以将指令的地址和可执行映像转换成文件名.函数名和源代码行数的工具.这种功能对于将跟踪地址转换成更有意义的内容来 ...
- Oracle 错误集锦
1.plsql进行更新操作时卡死的解决办法 https://blog.csdn.net/laoyingat/article/details/79379270
- PV对第三方存储的访问模式支持
访问模式 PV可以使用存储资源提供商支持的任何方法来映射到host中.如下的表格中所示,提供商有着不同的功能,每个PV的访问模式被设置为卷支持的指定模式.比如,NFS可以支持多个读/写的客户端,但可以 ...
- Monokai风格的EditPlus配色方案
EditPlus的配置文件editplus_u.ini,该文件默认在:系统盘:\Users\用户名\AppData\Roaming\EditPlus目录中.将其中的内容替换为如下即可: [Option ...