Description
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 <= 105sconsists of lowercase English letters.
Solutions
Solution 1: Dynamic Programming
We can define an array f of length 26, where f[i] represents the length of the longest consecutive substring ending with the ith character. The answer is the sum of all elements in f.
We define a variable k to represent the length of the longest consecutive substring ending with the current character. We iterate through the string s. For each character c, if the difference between c and the previous character s[i - 1] is 1, then we increment k by 1, otherwise, we reset k to 1. Then we update f[c] to be the larger value of f[c] and k.
Finally, we return the sum of all elements in f.
The time complexity is O(n), where n is the length of the string s. The space complexity is O(|Σ|), where Σ is the character set, in this case, the set of lowercase letters.
class Solution: def findSubstringInWraproundString(self, s: str) -> int: f = defaultdict(int) k = 0 for i, c in enumerate(s): if i and (ord(c) - ord(s[i - 1])) % 26 == 1: k += 1 else: k = 1 f[c] = max(f[c], k) return sum(f.values())(code-box)
