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的更多相关文章

  1. LeetCode 467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  2. 【LeetCode】467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  3. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  4. 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...

  5. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  6. Leetcode: Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  7. [Swift]LeetCode467. 环绕字符串中唯一的子字符串 | Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  8. 动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String

    2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将 ...

  9. 467. [leetcode] Unique Substrings in Wraparound String

    467. Unique Substrings in Wraparound String Implement atoi to convert a string to an integer. Hint: ...

随机推荐

  1. 「小程序JAVA实战」小程序的页面重定向(60)

    转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeyemianzhongdingxiang59/ 在我们正常的浏览网 ...

  2. tomcat没有发布maven项目依赖的本地jar包

    建立springMVC的maven项目,平时使用的jar包都是在pom.xml文件配置依赖关系, maven会自动从仓库中下载,这样使用tomcat部署发布都没有问题.但有时我们需要使用maven仓库 ...

  3. merage语句

    MERGE  INTO  [credit].[record_rule_data] AS a  USING @tem AS b  ON  a.user_gid =@userLogGid  AND a.r ...

  4. 优化 resolv.conf

    DNS lookup 在把域名解析成 IP 过程中耽误了不少时间,尤其是访问比较复杂的网站的时候,比如某个页面包含多个 url,一次请求需要做多次 DNS 解析,并且从用户浏览器到 DNS serve ...

  5. [Linux] Big-endian and Little-endian (大小端模式)

    Big-endian Little-endian 大小端模式   https://en.wikipedia.org/wiki/Endianness 大端模式,是指数据的高字节保存在内存的低地址中,而数 ...

  6. Java程序设计——对象序列化

    对象序列化的目标是将对象保存到磁盘中或允许在网络中直接传输对象,对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久保存在磁盘上,通过网络将这种二进制流传输到另 ...

  7. docker跨主机互联

    以下内容只是命令,原理自行百度,google或者官方查阅! 方案一.overlay Consul 三台主机为例(都要安装docker): 192.168.20.20(consul服务) 192.168 ...

  8. jar包制作一个可执行文件

    1.在桌面新建一个txt文件,然后修改为.bat后缀的文件,例如: 文件命名为:Editfact.bat 2.对文件内容进行编写,如下: %此处为bat文件盘符% c: %此处为jar包位置% cd ...

  9. tp5中url使用js变量传参方法

    window.location.href="{:url('Index/index')}>"+"/ID/"+ID; //这样可以生成,但url模式改变则不能 ...

  10. iOS隐藏导航条1px的底部横线

    第二种方法:1)声明UIImageView变量,存储底部横线 @implementation MyViewController { UIImageView *navBarHairlineImageVi ...