Skip to content

Commit e18424c

Browse files
committed
feat: add solutions to lc problems: No.0438,0713
- No.0438.Find All Anagrams in a String - No.0713.Subarray Product Less Than K
1 parent e7cf47c commit e18424c

File tree

7 files changed

+228
-0
lines changed

7 files changed

+228
-0
lines changed

solution/0400-0499/0438.Find All Anagrams in a String/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,38 @@ func findAnagrams(s string, p string) []int {
216216
}
217217
```
218218

219+
### **Rust**
220+
221+
```rust
222+
impl Solution {
223+
pub fn find_anagrams(s: String, p: String) -> Vec<i32> {
224+
let (s, p) = (s.as_bytes(), p.as_bytes());
225+
let (m, n) = (s.len(), p.len());
226+
let mut res = vec![];
227+
if n > m {
228+
return res;
229+
}
230+
231+
let mut counter = [0; 26];
232+
for i in 0..n {
233+
counter[(p[i] - b'a') as usize] += 1;
234+
counter[(s[i] - b'a') as usize] -= 1;
235+
}
236+
for i in n..m {
237+
if counter.iter().all(|&v| v == 0) {
238+
res.push((i - n) as i32);
239+
}
240+
counter[(s[i] - b'a') as usize] -= 1;
241+
counter[(s[i - n] - b'a') as usize] += 1;
242+
}
243+
if counter.iter().all(|&v| v == 0) {
244+
res.push((m - n) as i32);
245+
}
246+
res
247+
}
248+
}
249+
```
250+
219251
### **...**
220252

221253
```

solution/0400-0499/0438.Find All Anagrams in a String/README_EN.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,38 @@ func findAnagrams(s string, p string) []int {
172172
}
173173
```
174174

175+
### **Rust**
176+
177+
```rust
178+
impl Solution {
179+
pub fn find_anagrams(s: String, p: String) -> Vec<i32> {
180+
let (s, p) = (s.as_bytes(), p.as_bytes());
181+
let (m, n) = (s.len(), p.len());
182+
let mut res = vec![];
183+
if n > m {
184+
return res;
185+
}
186+
187+
let mut counter = [0; 26];
188+
for i in 0..n {
189+
counter[(p[i] - b'a') as usize] += 1;
190+
counter[(s[i] - b'a') as usize] -= 1;
191+
}
192+
for i in n..m {
193+
if counter.iter().all(|&v| v == 0) {
194+
res.push((i - n) as i32);
195+
}
196+
counter[(s[i] - b'a') as usize] -= 1;
197+
counter[(s[i - n] - b'a') as usize] += 1;
198+
}
199+
if counter.iter().all(|&v| v == 0) {
200+
res.push((m - n) as i32);
201+
}
202+
res
203+
}
204+
}
205+
```
206+
175207
### **...**
176208

177209
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
pub fn find_anagrams(s: String, p: String) -> Vec<i32> {
3+
let (s, p) = (s.as_bytes(), p.as_bytes());
4+
let (m, n) = (s.len(), p.len());
5+
let mut res = vec![];
6+
if n > m {
7+
return res;
8+
}
9+
10+
let mut counter = [0; 26];
11+
for i in 0..n {
12+
counter[(p[i] - b'a') as usize] += 1;
13+
counter[(s[i] - b'a') as usize] -= 1;
14+
}
15+
for i in n..m {
16+
if counter.iter().all(|&v| v == 0) {
17+
res.push((i - n) as i32);
18+
}
19+
counter[(s[i] - b'a') as usize] -= 1;
20+
counter[(s[i - n] - b'a') as usize] += 1;
21+
}
22+
if counter.iter().all(|&v| v == 0) {
23+
res.push((m - n) as i32);
24+
}
25+
res
26+
}
27+
}

solution/0700-0799/0713.Subarray Product Less Than K/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,55 @@ public:
8282

8383
```
8484

85+
### **TypeScript**
86+
87+
```ts
88+
function numSubarrayProductLessThanK(nums: number[], k: number): number {
89+
if (k <= 1) {
90+
return 0;
91+
}
92+
93+
const n = nums.length;
94+
let res = 0;
95+
for (let i = 0; i < n; i++) {
96+
let product = 1;
97+
for (let j = i; j < n; j++) {
98+
product *= nums[j];
99+
if (product >= k) {
100+
break;
101+
}
102+
res++;
103+
}
104+
}
105+
return res;
106+
}
107+
```
108+
109+
### **Rust**
110+
111+
```rust
112+
impl Solution {
113+
pub fn num_subarray_product_less_than_k(nums: Vec<i32>, k: i32) -> i32 {
114+
if k <= 1 {
115+
return 0;
116+
}
117+
118+
let mut res = 0;
119+
let mut product = 1;
120+
let mut i = 0;
121+
nums.iter().enumerate().for_each(|(j, v)| {
122+
product *= v;
123+
while product >= k {
124+
product /= nums[i];
125+
i += 1;
126+
}
127+
res += j - i + 1;
128+
});
129+
res as i32
130+
}
131+
}
132+
```
133+
85134
### **...**
86135

87136
```

solution/0700-0799/0713.Subarray Product Less Than K/README_EN.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,55 @@ public:
7474

7575
```
7676

77+
### **TypeScript**
78+
79+
```ts
80+
function numSubarrayProductLessThanK(nums: number[], k: number): number {
81+
if (k <= 1) {
82+
return 0;
83+
}
84+
85+
const n = nums.length;
86+
let res = 0;
87+
for (let i = 0; i < n; i++) {
88+
let product = 1;
89+
for (let j = i; j < n; j++) {
90+
product *= nums[j];
91+
if (product >= k) {
92+
break;
93+
}
94+
res++;
95+
}
96+
}
97+
return res;
98+
}
99+
```
100+
101+
### **Rust**
102+
103+
```rust
104+
impl Solution {
105+
pub fn num_subarray_product_less_than_k(nums: Vec<i32>, k: i32) -> i32 {
106+
if k <= 1 {
107+
return 0;
108+
}
109+
110+
let mut res = 0;
111+
let mut product = 1;
112+
let mut i = 0;
113+
nums.iter().enumerate().for_each(|(j, v)| {
114+
product *= v;
115+
while product >= k {
116+
product /= nums[i];
117+
i += 1;
118+
}
119+
res += j - i + 1;
120+
});
121+
res as i32
122+
}
123+
}
124+
```
125+
77126
### **...**
78127

79128
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn num_subarray_product_less_than_k(nums: Vec<i32>, k: i32) -> i32 {
3+
if k <= 1 {
4+
return 0;
5+
}
6+
7+
let mut res = 0;
8+
let mut product = 1;
9+
let mut i = 0;
10+
nums.iter().enumerate().for_each(|(j, v)| {
11+
product *= v;
12+
while product >= k {
13+
product /= nums[i];
14+
i += 1;
15+
}
16+
res += j - i + 1;
17+
});
18+
res as i32
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function numSubarrayProductLessThanK(nums: number[], k: number): number {
2+
if (k <= 1) {
3+
return 0;
4+
}
5+
6+
const n = nums.length;
7+
let res = 0;
8+
for (let i = 0; i < n; i++) {
9+
let product = 1;
10+
for (let j = i; j < n; j++) {
11+
product *= nums[j];
12+
if (product >= k) {
13+
break;
14+
}
15+
res++;
16+
}
17+
}
18+
return res;
19+
}

0 commit comments

Comments
 (0)