|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Medium |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3817.Good%20Indices%20in%20a%20Digit%20String/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3817. Good Indices in a Digit String 🔒](https://leetcode.com/problems/good-indices-in-a-digit-string) |
| 10 | + |
| 11 | +[中文文档](/solution/3800-3899/3817.Good%20Indices%20in%20a%20Digit%20String/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given a string <code>s</code> consisting of digits.</p> |
| 18 | + |
| 19 | +<p>An index <code>i</code> is called <strong>good</strong> if there exists a <span data-keyword="substring-nonempty">substring</span> of <code>s</code> that ends at index <code>i</code> and is equal to the decimal representation of <code>i</code>.</p> |
| 20 | + |
| 21 | +<p>Return an integer array of all good indices in <strong>increasing order</strong>.</p> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | +<p><strong class="example">Example 1:</strong></p> |
| 25 | + |
| 26 | +<div class="example-block"> |
| 27 | +<p><strong>Input:</strong> <span class="example-io">s = "0234567890112"</span></p> |
| 28 | + |
| 29 | +<p><strong>Output:</strong> <span class="example-io">[0,11,12]</span></p> |
| 30 | + |
| 31 | +<p><strong>Explanation:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li> |
| 35 | + <p>At index 0, the decimal representation of the index is <code>"0"</code>. The substring <code>s[0]</code> is <code>"0"</code>, which matches, so index <code>0</code> is good.</p> |
| 36 | + </li> |
| 37 | + <li> |
| 38 | + <p>At index 11, the decimal representation is <code>"11"</code>. The substring <code>s[10..11]</code> is <code>"11"</code>, which matches, so index <code>11</code> is good.</p> |
| 39 | + </li> |
| 40 | + <li> |
| 41 | + <p>At index 12, the decimal representation is <code>"12"</code>. The substring <code>s[11..12]</code> is <code>"12"</code>, which matches, so index <code>12</code> is good.</p> |
| 42 | + </li> |
| 43 | +</ul> |
| 44 | + |
| 45 | +<p>No other index has a substring ending at it that equals its decimal representation. Therefore, the answer is <code>[0, 11, 12]</code>.</p> |
| 46 | +</div> |
| 47 | + |
| 48 | +<p><strong class="example">Example 2:</strong></p> |
| 49 | + |
| 50 | +<div class="example-block"> |
| 51 | +<p><strong>Input:</strong> <span class="example-io">s = "01234"</span></p> |
| 52 | + |
| 53 | +<p><strong>Output:</strong> <span class="example-io">[0,1,2,3,4]</span></p> |
| 54 | + |
| 55 | +<p><strong>Explanation:</strong></p> |
| 56 | + |
| 57 | +<p>For every index <code>i</code> from 0 to 4, the decimal representation of <code>i</code> is a single digit, and the substring <code>s[i]</code> matches that digit.</p> |
| 58 | + |
| 59 | +<p>Therefore, a valid substring ending at each index exists, making all indices good.</p> |
| 60 | +</div> |
| 61 | + |
| 62 | +<p><strong class="example">Example 3:</strong></p> |
| 63 | + |
| 64 | +<div class="example-block"> |
| 65 | +<p><strong>Input:</strong> <span class="example-io">s = "12345"</span></p> |
| 66 | + |
| 67 | +<p><strong>Output:</strong> <span class="example-io">[]</span></p> |
| 68 | + |
| 69 | +<p><strong>Explanation:</strong></p> |
| 70 | + |
| 71 | +<p>No index has a substring ending at it that matches its decimal representation.</p> |
| 72 | + |
| 73 | +<p>Therefore, there are no good indices and the result is an empty array.</p> |
| 74 | +</div> |
| 75 | + |
| 76 | +<p> </p> |
| 77 | +<p><strong>Constraints:</strong></p> |
| 78 | + |
| 79 | +<ul> |
| 80 | + <li><code>1 <= s.length <= 10<sup>5</sup></code></li> |
| 81 | + <li><code>s</code> only consists of digits from <code>'0'</code> to <code>'9'</code>.</li> |
| 82 | +</ul> |
| 83 | + |
| 84 | +<!-- description:end --> |
| 85 | + |
| 86 | +## Solutions |
| 87 | + |
| 88 | +<!-- solution:start --> |
| 89 | + |
| 90 | +### Solution 1: Simulation |
| 91 | + |
| 92 | +We observe that the maximum length of string $s$ is $10^5$, and the length of the decimal representation of index $i$ is at most $6$ (since the decimal representation of $10^5$ is $100000$, which has a length of $6$). Therefore, we only need to check for each index $i$ whether the substring corresponding to its decimal representation is equal to it. |
| 93 | + |
| 94 | +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$, ignoring the space required for the answer. |
| 95 | + |
| 96 | +<!-- tabs:start --> |
| 97 | + |
| 98 | +#### Python3 |
| 99 | + |
| 100 | +```python |
| 101 | +class Solution: |
| 102 | + def goodIndices(self, s: str) -> List[int]: |
| 103 | + ans = [] |
| 104 | + for i in range(len(s)): |
| 105 | + t = str(i) |
| 106 | + k = len(t) |
| 107 | + if s[i + 1 - k : i + 1] == t: |
| 108 | + ans.append(i) |
| 109 | + return ans |
| 110 | +``` |
| 111 | + |
| 112 | +#### Java |
| 113 | + |
| 114 | +```java |
| 115 | +class Solution { |
| 116 | + public List<Integer> goodIndices(String s) { |
| 117 | + List<Integer> ans = new ArrayList<>(); |
| 118 | + for (int i = 0; i < s.length(); i++) { |
| 119 | + String t = String.valueOf(i); |
| 120 | + int k = t.length(); |
| 121 | + if (s.substring(i + 1 - k, i + 1).equals(t)) { |
| 122 | + ans.add(i); |
| 123 | + } |
| 124 | + } |
| 125 | + return ans; |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +#### C++ |
| 131 | + |
| 132 | +```cpp |
| 133 | +class Solution { |
| 134 | +public: |
| 135 | + vector<int> goodIndices(string s) { |
| 136 | + vector<int> ans; |
| 137 | + for (int i = 0; i < s.size(); i++) { |
| 138 | + string t = to_string(i); |
| 139 | + int k = t.size(); |
| 140 | + if (s.substr(i + 1 - k, k) == t) { |
| 141 | + ans.push_back(i); |
| 142 | + } |
| 143 | + } |
| 144 | + return ans; |
| 145 | + } |
| 146 | +}; |
| 147 | +``` |
| 148 | +
|
| 149 | +#### Go |
| 150 | +
|
| 151 | +```go |
| 152 | +func goodIndices(s string) (ans []int) { |
| 153 | + for i := range s { |
| 154 | + t := strconv.Itoa(i) |
| 155 | + k := len(t) |
| 156 | + if s[i+1-k:i+1] == t { |
| 157 | + ans = append(ans, i) |
| 158 | + } |
| 159 | + } |
| 160 | + return |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +#### TypeScript |
| 165 | + |
| 166 | +```ts |
| 167 | +function goodIndices(s: string): number[] { |
| 168 | + const ans: number[] = []; |
| 169 | + for (let i = 0; i < s.length; i++) { |
| 170 | + const t = String(i); |
| 171 | + const k = t.length; |
| 172 | + if (s.slice(i + 1 - k, i + 1) === t) { |
| 173 | + ans.push(i); |
| 174 | + } |
| 175 | + } |
| 176 | + return ans; |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +<!-- tabs:end --> |
| 181 | + |
| 182 | +<!-- solution:end --> |
| 183 | + |
| 184 | +<!-- problem:end --> |
0 commit comments