467. Unique Substrings in Wraparound String
来源:互联网 发布:高速摄像机知乎 编辑:程序博客网 时间:2024/06/05 02:10
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: 1Explanation: Only the substring "a" of string "a" is in the string s.
Example 2:
Input: "cac"Output: 2Explanation: There are two substrings "a", "c" of string "cac" in the string s.
Example 3:
Input: "zab"Output: 6Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.这道题用count[26]存储以a-z开头的最长子序列,最后累加count,得到结果。分析如下:
The idea is, if we know the max number of unique substrings in p
ends with 'a', 'b', ..., 'z'
, then the summary of them is the answer. Why is that?
- 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 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. - No matter how long is a contiguous substring in
p
, it is ins
sinces
has infinite length. - Now we know the max number of unique substrings in
p
ends with'a', 'b', ..., 'z'
and those substrings are all ins
. Summary is the answer, according to the question.
public class Solution { public int findSubstringInWraproundString(String p) { int[] count = new int[26]; char[] chs = p.toCharArray(); int maxLength = 1; for (int i = 0; i < chs.length; i ++) { if (i > 0 && (chs[i] - chs[i - 1] == 1 || chs[i - 1] - chs[i] == 25)) { maxLength ++; } else { maxLength = 1; } int index = chs[i] - 'a'; count[index] = Math.max(maxLength, count[index]); } int sum = 0; for (int n: count) { sum += n; } return sum; }}
- 467. Unique Substrings in Wraparound String**
- 467. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String
- Leetcode-467. Unique Substrings in Wraparound String
- Leetcode 467. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String
- 【LeetCode】467. Unique Substrings in Wraparound String
- Leetcode-467. Unique Substrings in Wraparound String
- [LeetCode]467. Unique Substrings in Wraparound String
- **LeetCode 467. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String -Medium
- 【LeetCode】467. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String
- 467. Unique Substrings in Wraparound String Medium
- 支持ajax 跨域调用的WCF搭建
- Struts2 接收参数的三种方式
- springmvc利用validation框架实现数据校验
- POJ2027No Brainer
- SQL消费表中查找所有用户最后一条消费记录
- 467. Unique Substrings in Wraparound String
- Opencv获取最小外接矩形——minAreaRect
- underscore.js
- react中使用AntDesign库 --- babel-plugin-import 配置
- 二分图
- ios开发之NSEnumerator
- php.ini 5.6解释
- 【LeetCode】5. Longest Palindromic Substring
- spark一千篇旅游日记0006 之 DataFrame(二)