Advertisement

Unique Substrings in Wraparound String - LeetCode 467 Solution

Unique Substrings in Wraparound String - Complete Solution Guide

Unique Substrings in Wraparound String is LeetCode problem 467, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem asks us to find the number of unique non-empty substrings of a given string `s` that are present in an infinite wraparound string `base` made up of the alphabet 'abcdefghijklmnopqrstuvwxyz' repeated infinitely. In essence, we want to find out how many distinct substrings from `s` exist within this infinite wraparound string. For instance, 'za' and 'ab' are valid consecutive substrings in the wraparound string, as is 'zab'. The input is a string `s` containing only lowercase English letters, and the output is an integer representing the count of unique substrings of `s` found in the wraparound string. The length of the input string `s` can be up to 10^5.

Solution Approach

The solution uses a dynamic programming approach to efficiently calculate the number of unique substrings. It iterates through the input string `s`, keeping track of the current length of a consecutive wraparound sequence. For each character, it updates an array `maxLength` where `maxLength[i]` stores the maximum length of a wraparound substring ending with the i-th character of the alphabet (where 'a' is the 0th character, 'b' is the 1st, etc.). This array helps prevent double-counting substrings. The final result is the sum of all values in the `maxLength` array, which represents the total number of unique substrings.

Step-by-Step Algorithm

  1. Step 1: Initialize an array `maxLength` of size 26 with all elements set to 0. This array will store the maximum length of a wraparound string ending at each letter (a-z).
  2. Step 2: Initialize a variable `currentLength` to 0. This variable tracks the length of the current consecutive wraparound string.
  3. Step 3: Iterate through the input string `s` from left to right.
  4. Step 4: In each iteration, check if the current character is a consecutive character in the wraparound string compared to the previous character. This is determined by checking if `(ord(s[i]) - ord(s[i-1])) % 26 == 1`. If it is, increment `currentLength`. Otherwise, reset `currentLength` to 1.
  5. Step 5: Calculate the index `idx` corresponding to the current character `s[i]` in the `maxLength` array. This index is `ord(s[i]) - ord('a')`.
  6. Step 6: Update `maxLength[idx]` to be the maximum of its current value and `currentLength`. This ensures that we store the longest wraparound string ending with this letter.
  7. Step 7: After iterating through the entire string `s`, calculate the sum of all elements in the `maxLength` array. This sum represents the total number of unique substrings in the wraparound string that are also substrings of `s`.
  8. Step 8: Return the calculated sum.

Key Insights

  • Insight 1: We don't need to actually generate the infinite wraparound string. We only need to consider the consecutive subsequences (substrings) within the input string `s`.
  • Insight 2: For each character in `s`, we only need to consider the longest consecutive wraparound sequence ending at that character. This reduces the number of substrings to check dramatically.
  • Insight 3: We can use dynamic programming to keep track of the maximum length of the consecutive wraparound sequences ending at each character of the alphabet. A hashmap/array of size 26 indexed by the alphabet can achieve this.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: String, Dynamic Programming.

Companies

Asked at: MAQ Software.