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. FireMoneky 画图 Point 赋值

    VCL 的 Canvas.Pen 对应FMX: Canvas.Stroke;VCL到 Canvas.Brush 对应FMX: Canvas.Fill. TCircle 圆形控件 Inkscape 0. ...

  2. JAVA 获取文件的MD5值大小以及常见的工具类

    /** * 获取文件的MD5值大小 * * @param file * 文件对象 * @return */ public static String getMD5(File file) { FileI ...

  3. 实现HBase增量入库(HBase删除自定义时间戳行数据)

    目录 1. 背景描述 2. 问题描述 3. 解决方案 1. 背景描述 目前在做音乐推荐项目,前期做排序模型优化,任务是使用模型对用户的历史音乐进行排序,有6800多万个用户,约40G的用户数据,使用H ...

  4. Linux中shell变量$0,$?等含义

    linux中shell变量$#,$@,$0,$1,$2的基本含义: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  5. ShaderForge打造自定义光照模型

    [ShaderForge打造自定义光照模型] 1.Lambert逻辑图. 2.Blinn-Phong逻辑图. 参考:https://www.youtube.com/watch?v=EjCXwV0YYd ...

  6. c语言静态断言

    在php中可以通过xdebug来显示详细的错误信息,可以细化到哪个文件哪行代码引起的报错.在C语言里面也可以通过静态断言(assert)来使得调试代码更加方便.关于断言,可以作为一种很强大的调试方式或 ...

  7. 一些json在js和c++ jsoncpp的操作

    1.对于javascript部分,如果将字符串转为json对象? var aa ={ keyword:"zoumm", requestcount:"5", ne ...

  8. QTcpSocket-Qt使用Tcp通讯实现服务端和客户端

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QTcpSocket-Qt使用Tcp通讯实现服务端和客户端     本文地址:https:// ...

  9. POP邮件收取邮件 代码

    // 111111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <WinSock.h> #include ...

  10. 让Ubuntu使用阿里云国内源,解决下载速度慢问题。

    阿里云镜像官方地址 http://mirrors.aliyun.com/ 找到最新源地址列表: http://www.linuxdiyf.com/linux/23163.html 软件包管理中心(推荐 ...