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.

这道题我一开始发现了数学规律:

length of consecutive chars   vs   number of different substring

“a” -> 1 = 1

"ab" -> 3 = 1+2

"abc" -> 6 = 1+2+3

"abcd" -> 10 = 1+2+3+4

于是打算计算string p里面每一段consecutive substring的length是多少,比如abcxyz, 两段分别为“abc”“xyz”, len分别为3和3,#of substring分别是6和6,总数是6+6。 但是这个方法没法处理有重复的,比如abczab,"abc"依然能产生6个substring, 然而"zab"就有重复了,只能产生3个different substring.

如何处理重复呢? “abc”和“zab”里面的"ab"要同时考虑到,于是就有了DP的想法:

total number of different substring = sum of {number of different substring end by each char}

Further,

number of different substring end by each char =  length of longest contiguous substring ends with that letter. 

Example "abcd", the  number of unique substring ends with 'd' is 4, apparently they are "abcd", "bcd", "cd" and "d".

If there 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.

 public class Solution {
public int findSubstringInWraproundString(String p) {
if (p==null || p.length()==0) return 0;
int[] nums = new int[26]; //numbers of different substrings end with a specific char int longestConsec = 0;
for (int i=0; i<p.length(); i++) {
if (i>0 && (p.charAt(i-1)+1 == p.charAt(i) || p.charAt(i-1)-25 == p.charAt(i))) {
longestConsec++;
}
else longestConsec = 1; char c = p.charAt(i);
nums[(int)(c-'a')] = Math.max(nums[(int)(c-'a')], longestConsec);
} int res = 0;
for (int num : nums) {
res += num;
}
return res;
}
}

Leetcode: Unique Substrings in Wraparound String的更多相关文章

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

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

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

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

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

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

  4. LeetCode 467. Unique Substrings in Wraparound String

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

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

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

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

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

  7. 467. 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 Unique Substrings in Wraparound String 封装字符串中的独特子字符串

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

随机推荐

  1. 如何设置游戏分辨率(C++)

  2. 关于activity的生命周期的随笔

    在activity的生命周期中,我总是容易混淆,onstart和on resume ,on pause和onstop 原来这个都是一对的. onstart 对应 onstop ,意义在于使页面显示出来 ...

  3. Service Locator 服务定位模式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. js面向对象总结(一)

    在 js 中,对象由特性(attribute)构成,特性可以是原始值,也可以是引用值.如果特性存放的是函数,它将被看作对象的方法(method),否则该特性被看作对象的属性(property).在js ...

  5. substr 与 substring 的区别

    substr (start[, 所要子川的长度]); substring(start, 结束的位置)

  6. Linux文件系统扩容步骤

    1 扩容前检查 cat /etc/fstab df -h 在扩容之前请确认VG的Free大小,以及文件和文件系统是否达到系统限制 2 系统识别硬盘 #echo "- - -" &g ...

  7. tableview 位置发生偏移

    状况描述:1.首次进入该界面时正常 2.push了新的界面后,再返回该界面 tableview和导航栏直接出现了间隔区域 tableview为代码创建 _tableView =  [[UITableV ...

  8. 使用Git进行项目管理

    首先在https://git.oschina.net进行注册以及登陆 登陆进去之后,如果想要创建项目,可以在 点击加号按钮,进行项目创建 3.这里以创建私有项目为例: 输入完成后,点击“创建”,进入下 ...

  9. WPF整理--动态绑定到Logical Resource

    “What happens if we replace aspecific resource? Would that be reflected in all objects using the res ...

  10. Random随机类(11选5彩票)BigInteger大数据类(华为面试题1000的阶乘)

    先上Java Web图 为了简化叙述,只写Java代码,然后控制台输出 使用[Random类]取得随机数 import java.util.Random; public class Fir { pub ...