Skip to content

Commit 4b4c2e0

Browse files
authored
feat: add solutions for lc No.3817 (#4984)
1 parent 25202d1 commit 4b4c2e0

11 files changed

Lines changed: 429 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
- [乘积小于 K 的子数组](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README.md) - `双指针`
5353
- [位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md) - `位运算``lowbit`
5454
- [合并区间](/solution/0000-0099/0056.Merge%20Intervals/README.md) - `区间合并`
55-
<!-- 排序算法、待补充 -->
55+
<!-- 排序算法、待补充 -->
5656

5757
### 2. 数据结构
5858

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3817.Good%20Indices%20in%20a%20Digit%20String/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3817. Good Indices in a Digit String 🔒](https://leetcode.cn/problems/good-indices-in-a-digit-string)
10+
11+
[English Version](/solution/3800-3899/3817.Good%20Indices%20in%20a%20Digit%20String/README_EN.md)
12+
13+
## 题目描述
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>&nbsp;</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 = &quot;0234567890112&quot;</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>&quot;0&quot;</code>. The substring <code>s[0]</code> is <code>&quot;0&quot;</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>&quot;11&quot;</code>. The substring <code>s[10..11]</code> is <code>&quot;11&quot;</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>&quot;12&quot;</code>. The substring <code>s[11..12]</code> is <code>&quot;12&quot;</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 = &quot;01234&quot;</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 = &quot;12345&quot;</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>&nbsp;</p>
77+
<p><strong>Constraints:</strong></p>
78+
79+
<ul>
80+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
81+
<li><code>s</code> only consists of digits from <code>&#39;0&#39;</code> to <code>&#39;9&#39;</code>.</li>
82+
</ul>
83+
84+
<!-- description:end -->
85+
86+
## 解法
87+
88+
<!-- solution:start -->
89+
90+
### 方法一:模拟
91+
92+
我们注意到,字符串 $s$ 的长度最大为 $10^5$,而索引 $i$ 的十进制表示的长度最多为 $6$(因为 $10^5$ 的十进制表示为 $100000$,长度为 $6$)。因此,我们只需要检查每个索引 $i$ 的十进制表示对应的子串是否与之相等。
93+
94+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
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 -->
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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>&nbsp;</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 = &quot;0234567890112&quot;</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>&quot;0&quot;</code>. The substring <code>s[0]</code> is <code>&quot;0&quot;</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>&quot;11&quot;</code>. The substring <code>s[10..11]</code> is <code>&quot;11&quot;</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>&quot;12&quot;</code>. The substring <code>s[11..12]</code> is <code>&quot;12&quot;</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 = &quot;01234&quot;</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 = &quot;12345&quot;</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>&nbsp;</p>
77+
<p><strong>Constraints:</strong></p>
78+
79+
<ul>
80+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
81+
<li><code>s</code> only consists of digits from <code>&#39;0&#39;</code> to <code>&#39;9&#39;</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 -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<int> goodIndices(string s) {
4+
vector<int> ans;
5+
for (int i = 0; i < s.size(); i++) {
6+
string t = to_string(i);
7+
int k = t.size();
8+
if (s.substr(i + 1 - k, k) == t) {
9+
ans.push_back(i);
10+
}
11+
}
12+
return ans;
13+
}
14+
};

0 commit comments

Comments
 (0)