Watch the video solution for Good Indices in a Digit String, a medium level problem involving Math, String. This walkthrough by Code-Yao has 29 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s consisting of digits.
An index i is called good if there exists a substring of s that ends at index i and is equal to the decimal representation of i.
Return an integer array of all good indices in increasing order.
Example 1:
Input: s = "0234567890112"
Output: [0,11,12]
Explanation:
At index 0, the decimal representation of the index is "0". The substring s[0] is "0", which matches, so index 0 is good.
At index 11, the decimal representation is "11". The substring s[10..11] is "11", which matches, so index 11 is good.
At index 12, the decimal representation is "12". The substring s[11..12] is "12", which matches, so index 12 is good.
No other index has a substring ending at it that equals its decimal representation. Therefore, the answer is [0, 11, 12].
Example 2:
Input: s = "01234"
Output: [0,1,2,3,4]
Explanation:
For every index i from 0 to 4, the decimal representation of i is a single digit, and the substring s[i] matches that digit.
Therefore, a valid substring ending at each index exists, making all indices good.
Example 3:
Input: s = "12345"
Output: []
Explanation:
No index has a substring ending at it that matches its decimal representation.
Therefore, there are no good indices and the result is an empty array.
Constraints:
1 <= s.length <= 105s only consists of digits from '0' to '9'.Problem Overview: You are given a string of digits. An index is considered good if the sum of digits on its left equals the sum of digits on its right. The task is to scan the string and return all indices that satisfy this balance condition.
Approach 1: Brute Force Simulation (O(n^2) time, O(1) space)
Check every index independently. For each position i, iterate over the substring [0..i-1] to compute the left digit sum and over [i+1..n-1] to compute the right digit sum. If the two sums match, record the index as good. This approach uses plain iteration and no extra data structures, but it recomputes the same sums repeatedly, leading to quadratic time for long strings.
Approach 2: Prefix Sum Simulation (O(n) time, O(1) space)
Precompute the total sum of all digits. While iterating through the string, maintain a running leftSum. For index i, compute the right side using rightSum = totalSum - leftSum - digit[i]. If leftSum == rightSum, the index is good. After the check, update leftSum += digit[i]. This eliminates repeated summation and reduces the complexity to linear time. The technique is a standard optimization when dealing with cumulative values in math and string problems.
The key insight is that you never need to recompute sums for each side of the index. Maintaining a running prefix value and deriving the suffix value from the total keeps the algorithm efficient and easy to implement.
Recommended for interviews: The prefix-sum simulation is the expected solution. Starting with the brute force method shows you understand the definition of a good index. Transitioning to the prefix-based optimization demonstrates awareness of common mathematical accumulation patterns and how to reduce repeated work from O(n^2) to O(n).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(n^2) | O(1) | Useful for understanding the definition of a good index or when input size is very small |
| Prefix Sum Simulation | O(n) | O(1) | General case; avoids repeated summation and is the optimal interview solution |