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: ...
随机推荐
- Vue 实现微信 jssdk 扫码, 上传图片
流程 1: 配置微信公众号JS域名 2:前端发送URL后台获取JSSDK配置, 后台Service代码如下, 修改2处位置: WeixinUtil.APPID => 当前公众号APPID Wei ...
- FM模型
[ 闲聊DNN CTR预估模型] http://www.mamicode.com/info-detail-1465813.html http://blog.csdn.net/bitcarmanlee/ ...
- PHP 将HTML转成PDF文件,HTML 转word,Word 转Html
html转 PDF 用MPDF ,Word转Html 纯PHP的方法暂未找到Word直接转PDF的方法 可以使用 LibreOffice 6.1 (Liunx,win) 皆可使用 Html转成PD ...
- jsp页面拨打电话和QQ聊天
拨打电话: <a href="tel:手机号">拨打电话</a> 这种方式塞班.安卓与iphone都支持. 参考文章:https://blog.csdn.n ...
- 8-matlab-gui-显示图片有坐标刻度问题
在图片上显示图片时,总是有图片,一遍做法是使得刻度为空就可了: 在你的每一个axes的CreateFcn函数中添加一下代码即可:set(hObject,'xTick',[]);set(hObject, ...
- Gcc And MakeFile Level1
简单介绍gcc And make 的使用 基本编译 gcc a.c b.c -o exeName 分步编译 gcc -c a.c -o a.o gcc a.o b.c -o main.o 使用Make ...
- ireport related
http://blog.csdn.net/q326527970/article/details/7049047
- Linux CentOS6 mysql rpm安装
mysql rpm文件下载地址:https://dev.mysql.com/downloads/mysql/ 选择操作系统及版本: 点击5.6版本链接 将下载好的文件上传至服务器 下面开始安装: 检查 ...
- [OS]windows 2012 server-Local users and groups-Backup Operators
怎样找到windows 2012 server上的Backup Operators Press the Windows + R keys to open the Run dialog, type lu ...
- mysql 字段名是关键字 报错
DROP TABLE IF EXISTS `bas_dictionary`; CREATE TABLE `bas_dictionary` ( `id` ) NOT NULL AUTO_INCREMEN ...