Skip to content

Commit 0bc9eb2

Browse files
committed
feat: add rust solution to lc problem: No.1016
1 parent 3744fa2 commit 0bc9eb2

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

solution/1000-1099/1016.Binary String With Substrings Representing 1 To N/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ function queryString(s: string, n: number): boolean {
148148
}
149149
```
150150

151+
#### Rust
152+
153+
```rust
154+
func queryString(s string, n int) bool {
155+
if n > 1000 {
156+
return false
157+
}
158+
for i := n; i > n/2; i-- {
159+
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
160+
return false
161+
}
162+
}
163+
return true
164+
}
165+
```
166+
151167
<!-- tabs:end -->
152168

153169
<!-- solution:end -->

solution/1000-1099/1016.Binary String With Substrings Representing 1 To N/README_EN.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ tags:
4848

4949
<!-- solution:start -->
5050

51-
### Solution 1
51+
### Solution 1: Brain Teaser
52+
53+
We observe that the length of string $s$ does not exceed $1000$, so string $s$ can represent at most $1000$ binary integers. Therefore, if $n \gt 1000$, then $s$ definitely cannot represent the binary representation of all integers in the range $[1,.. n]$.
54+
55+
Additionally, for an integer $x$, if the binary representation of $x$ is a substring of $s$, then the binary representation of $\lfloor x / 2 \rfloor$ is also a substring of $s$. Therefore, we only need to check whether the binary representations of integers in the range $[\lfloor n / 2 \rfloor + 1,.. n]$ are substrings of $s$.
56+
57+
The time complexity is $O(m^2 \times \log m)$ and the space complexity is $O(\log n)$, where $m$ is the length of string $s$ and $n$ is the positive integer given in the problem.
5258

5359
<!-- tabs:start -->
5460

@@ -133,6 +139,22 @@ function queryString(s: string, n: number): boolean {
133139
}
134140
```
135141

142+
#### Rust
143+
144+
```rust
145+
func queryString(s string, n int) bool {
146+
if n > 1000 {
147+
return false
148+
}
149+
for i := n; i > n/2; i-- {
150+
if !strings.Contains(s, strconv.FormatInt(int64(i), 2)) {
151+
return false
152+
}
153+
}
154+
return true
155+
}
156+
```
157+
136158
<!-- tabs:end -->
137159

138160
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn query_string(s: String, n: i32) -> bool {
3+
if n > 1000 {
4+
return false;
5+
}
6+
for i in (n / 2 + 1..=n).rev() {
7+
if !s.contains(&format!("{:b}", i)) {
8+
return false;
9+
}
10+
}
11+
true
12+
}
13+
}

0 commit comments

Comments
 (0)