JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

We define the string s to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz”, so s will look like this:

  • “…zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd….”.

Given a string p, return the number of unique non-empty substrings of p are present in s.

Example 1:

Input: p = "a"
Output: 1
Explanation: Only the substring "a" of p is in s.

Example 2:

Input: p = "cac"
Output: 2
Explanation: There are two substrings ("a", "c") of p in s.

Example 3:

Input: p = "zab"
Output: 6
Explanation: There are six substrings ("z", "a", "b", "za", "ab", and "zab") of p in s.

Constraints:

  • 1 <= p.length <= 105
  • p consists of lowercase English letters.

Code

class Solution {
    public int findSubstringInWraproundString(String p) {
        int[] dict = new int[26];
        int len = 0;
        
        for(int i = 0; i < p.length(); i++){
            char curr = p.charAt(i);
            
            if(i > 0 && (curr - p.charAt(i - 1) == 1 || (p.charAt(i - 1) == 'z' && curr == 'a'))){
                len++;
            } else {
                len = 1;
            }
            
            dict[curr - 'a'] = Math.max(dict[curr - 'a'], len);
        }
        
        int res = 0;
        for(int num : dict){
            res += num;
        }
        
        return res;
    }
}