#467

Unique Substrings in Wraparound String

specialist · 985 · lc medium +32 · verified · 42.7% accepted · 1,528 likes · top 24%

play →

Description

Define the infinite string base as the English lowercase alphabet repeated cyclically without end: "...xyzabcdefghijklmnopqrstuvwxyzabc...". Given string s, return the number of distinct non-empty substrings of s that are also substrings of base.

Example 1:

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

Example 2:

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

Example 3:

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

Code

1
2
3