467. Unique Substrings in Wraparound String

网友投稿 506 2022-11-11

467. Unique Substrings in Wraparound String

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

Example 2:

Input: "cac"Output: 2Explanation: There are two substrings "a", "c" of string "cac" in the string

Example 3:

Input: "zab"Output: 6Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string

思路: 找出以’a-z’每个字符结尾的情况下,最长的子串有多长,然后将其相加就可以。

class Solution public int findSubstringInWraproundString(String p) { int p_int[] = new int[p.length()]; int count[] = new int[26]; for(int i = 0; i < p.length(); i++){ p_int[i] = p.charAt(i) - 'a'; } int res = 0; int maxLen = 0; for(int i = 0; i < p.length(); i++ ){ if( i > 0 && (p_int[i-1] + 1) % 26 == p_int[i]){ maxLen ++ ; } else{ maxLen = 1; } count[p_int[i]] = Math.max(count[p_int[i]], maxLen); } for(int i = 0;i < 26;i++) res += count[i]; return

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于Maven pom文件中属性变量总结
下一篇:仿element-ui改造file类型input框的默认CSS
相关文章

 发表评论

暂时没有评论,来抢沙发吧~