LeetCode Unique Substrings in Wraparound String

原题链接在这里:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/

题目:

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.

题解:

以题中"zab"为例. 以‘z‘为尾的最长连续substring长度是1. 只有一个substring "z".

以‘a‘为尾的最长连续substring长度是2. "za" 和 "a".

以‘b‘为尾的最长连续substring长度是3. "zab", "ab" 和 "b".

最长连续以这个letter 结尾的substring长度恰巧是max number of unique substring ends with这个letter.

如果后面有overlap也没有关系只要维护住最长的长度.

最后每个letter的max number of unique substring的和就是答案.

Time Complexity: O(p.length()). Space: O(1).

AC Java:

 1 class Solution {
 2     public int findSubstringInWraproundString(String p) {
 3         int [] dp = new int[26];
 4         int maxCount = 0;
 5         for(int i = 0; i<p.length(); i++){
 6             if(i>0 && (p.charAt(i)-p.charAt(i-1)==1 || p.charAt(i-1)-p.charAt(i)==25)){
 7                 maxCount++;
 8             }else{
 9                 maxCount = 1;
10             }
11
12             dp[p.charAt(i)-‘a‘] = Math.max(dp[p.charAt(i)-‘a‘], maxCount);
13         }
14
15         int sum = 0;
16         for(int n : dp){
17             sum += n;
18         }
19         return sum;
20     }
21 }
时间: 2024-11-07 13:57:27

LeetCode Unique Substrings in Wraparound String的相关文章

[LeetCode] 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

[Leetcode] DP-- 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

467. Unique Substrings in Wraparound String

https://leetcode.com/problems/unique-substrings-in-wraparound-string/#/description Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefgh

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

2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将每个字符结尾的最长长度进行保存,这样就巧妙的解决的重复的问题. The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that

LeetCode &quot;467. Unique Substrings in Wraparound String&quot; !!

Check out this brilliant solution:https://discuss.leetcode.com/topic/70658/concise-java-solution-using-dp My first solution was a O(n^2) typical DP boiler-plate code. The key is, that solution is too generalized. With given char as the ending char in

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

详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solution { public: int findSubstringInWraproundString(string p) { vector<int> cnt(26, 0); int len = 0; for (int i = 0; i < p.size(); ++i) { if (i >

LeetCode(387)First Unique Character in a String

题目 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowerc

Leetcode-387 First Unique Character in a String

#387.   First Unique Character in a String Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You m

LeetCode_387. First Unique Character in a String

387. First Unique Character in a String Easy Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You