878. Good Indices in a Digit String

You are provided 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 = "0123456789"

Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Explanation: For every index i from 0 to 9, the decimal representation of i is a single digit, and the substring s[i] exactly matches that digit.

Therefore, a valid substring ending at each index exists, making all indices good.

Example 2:

Input: s = "11231"

Output: [1, 2, 3]

Explanation:

  • At index 1, the decimal representation of the index is "1". The substring s[1] is "1", which matches, so index 1 is good.
  • At index 2, the decimal representation is "2". The substring s[2] is "2", which matches, so index 2 is good.
  • At index 3, the decimal representation is "3". The substring s[3] is "3", which matches, so index 3 is good.
  • For index 0 (needs "0") and index 4 (needs "4"), there are no valid substrings ending at those indices that match their decimal representations.

Therefore, the answer is [1, 2, 3].

Now Your Turn!

Pick the correct output for the given input

Input: s = "12231"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 105
  • s only consists of digits from '0' to '9'.

Hints

0
class Solution {
public:
vector<int> goodIndices(string s) {
// Your code goes here
}
};
Test Case

Input:

S