Talentd
FleetCode

467. Unique Substrings in Wraparound String

Medium44.0% acceptance2 companies

We define the string base to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so base will look like this:

  • "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".

Given a string s, return the number of unique non-empty substrings of s are present in 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.

Constraints:

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

Asked at 2 companies

Hints open one at a time

Hint 1
One possible solution might be to consider allocating an array size of 26 for each character in the alphabet. (Credits to @r2ysxu)
28:54
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def findSubstringInWraproundString(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"a"
Input
"cac"
Input
"zab"
Your code is saved on this device.Solution