467. Unique Substrings in Wraparound String

T: O(n)
S: O(1) 
```java
public class Solution {
    public int findSubstringInWraproundString(String p) {
        int[] count = new int[26];
        int maxSubarrayCount = 0;
        for (int i = 0; i < p.length(); i++) {
            if (i > 0 && (p.charAt(i) - p.charAt(i-1) == 1 || p.charAt(i-1) - p.charAt(i) == 25)) { // z(25) - a(0) = 25
                maxSubarrayCount++;
            } else {
                maxSubarrayCount = 1;
            }
            
            count[p.charAt(i) - 'a'] = Math.max(count[p.charAt(i) - 'a'], maxSubarrayCount);
        }
        int result = 0;
        for (int i = 0; i < 26; i++) {
            result += count[i];
        }
        return result;
    }
}

/**
T: O(n)
S: O(1) 


if (i > 0 && (p.charAt(i) - p.charAt(i-1) == 1 || p.charAt(i-1) - p.charAt(i) == 25)) { // z(25) - a(0) = 25
    maxSubarrayCount++; // why?
} else {
    maxSubarrayCount = 1;
}

why -> ans: this maxcur 持續使用..因為是連續一樣的話, 繼續累加就是 subarray 的總數量
ex: scan "zab""
start from z

count[z] = 1 // z (maxcur = 1)
count[a] = 2 // a, za (maxcur++, maxcur = 2)
count[b] = 3 // b, ab, zab (maxcur++, maxcur = 3)



"zaba"
count[z] = 1 // z
count[a] = 2 // a, za
count[b] = 3 // b, ab, zab

at last a -> check ba -> not match -> maxcur become 1, 
but there already a bigger one in count[a], so only remain 2
that's why needs this 
-> count[p.charAt(i) - 'a'] = Math.max(count[p.charAt(i) - 'a'], maxSubarrayCount);
 */
```

Last updated