This solution might look a bit verbose but it is straight forward. `dp[i]`

is the number of qualified substrings ending at the position `i`

in `p`

, `seen`

is a dictionary keyed by the character `c`

that maintains the greatest number of qualified substrings ending at the char `c`

.

```
class Solution(object):
def findSubstringInWraproundString(self, p):
"""
:type p: str
:rtype: int
"""
N = len(p)
if N <= 1: return N
dp = [0 for i in range(N)]
start, seen = 0, {}
dp[0], seen[p[0]] = 1, 1
for i in range(1, N):
if p[i - 1] == 'z' and p[i] == 'a' or ord(p[i - 1]) + 1 == ord(p[i]):
x = i - start + 1
if p[i] not in seen:
dp[i] = x
seen[p[i]] = dp[i]
else:
if x > seen[p[i]]:
dp[i] = x - seen[p[i]]
seen[p[i]] = x
else:
dp[i] = 0
else:
if p[i] not in seen:
dp[i] = 1
seen[p[i]] = dp[i]
else:
dp[i] = 0
start = i
return sum(dp)
```