<p>We define the string <code>base</code> to be the infinite wraparound string of <code>&quot;abcdefghijklmnopqrstuvwxyz&quot;</code>, so <code>base</code> will look like this:</p>

<ul>
	<li><code>&quot;...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....&quot;</code>.</li>
</ul>

<p>Given a string <code>s</code>, return <em>the number of <strong>unique non-empty substrings</strong> of </em><code>s</code><em> are present in </em><code>base</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;a&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> Only the substring &quot;a&quot; of s is in base.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;cac&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two substrings (&quot;a&quot;, &quot;c&quot;) of s in base.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;zab&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> There are six substrings (&quot;z&quot;, &quot;a&quot;, &quot;b&quot;, &quot;za&quot;, &quot;ab&quot;, and &quot;zab&quot;) of s in base.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class Solution:    
    idx_cache: Dict[Tuple[str, str], int] = dict()

    def getIdx(self, char: str, from_: str = 'a') -> int:
        self.idx_cache[(char, from_)] = res = ord(char) - ord(from_)
        return res

    @cache
    def get_indices(self, char: str, from_: str) -> Tuple[int, int]:
        try:
            idx = self.idx_cache[(char, 'a')]
        except KeyError:
            idx = self.getIdx(char = char)

        try:
            diff_idx = self.idx_cache[(char, from_)]
        except KeyError:
            diff_idx = self.getIdx(char = char, from_ = from_)
        return idx, diff_idx

    

    def findSubstringInWraproundString(self, s: str) -> int:                
        track_arr= [0]*26
        idx, _ = self.get_indices(char = s[0], from_ = 'a')
        current_count = track_arr[idx] = 1

        for i in range(1,len(s)):
            idx, diff_idx = self.get_indices(char = s[i], from_ = s[i-1])

            if diff_idx % 26 == 1:
                current_count += 1
            else: 
                current_count = 1

            if current_count > track_arr[idx]:
                track_arr[idx] = current_count
            
        return sum(track_arr)
18800