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: "...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.

dp.

用一个length数组记录每个字母作为最大子串的最后一个字母时的子串长度。

 1 class Solution {
 2 public:
 3     int findSubstringInWraproundString(string p) {
 4         vector<int> length(26,0);
 5         const int size=p.length();
 6         int len=0,num=0;
 7         for(int i=0;i<size;i++)
 8         {
 9             int cur=p[i]-‘a‘;
10             //比较现在的字母和前一个字母,(x+25)%26可以得到前一个字母(a为0...z为26)
11             if(i!=0&&p[i-1]!=(cur+25)%26+‘a‘)
12                 len=0;
13             if(++len>length[cur])//++表示每个字母至少为长度为1的子串的最后一个字母
14             {
15                 num+=len-length[cur];//由于s串固定,所以子串从最后一个字母往前推都是固定的,只是可以推多长不确定
16                 length[cur]=len;//当前字母作为最后一个字母的子串还可以增长
17             }
18         }
19         return num;
20     }
21 };
时间: 2024-10-14 12:20:53

467. Unique Substrings in Wraparound String的相关文章

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

[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 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: "...zabcdefghijklmnopqrstuvw

动态规划-独特的子字符串存在于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-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

题目 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 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