Skip to content

Commit b320360

Browse files
committedFeb 23, 2022
feat: add solutions to lc problem: No.0567
No.0567.Permutation in String
1 parent 42d1713 commit b320360

File tree

4 files changed

+282
-0
lines changed

4 files changed

+282
-0
lines changed
 

‎solution/0500-0599/0567.Permutation in String/README.md

+99
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,105 @@
5858

5959
```
6060

61+
### **TypeScript**
62+
63+
```ts
64+
function checkInclusion(s1: string, s2: string): boolean {
65+
// 滑动窗口方案
66+
if (s1.length > s2.length) {
67+
return false;
68+
}
69+
70+
const n = s1.length;
71+
const m = s2.length;
72+
73+
const toCode = (s: string) => s.charCodeAt(0) - 97;
74+
const isMatch = () => {
75+
for (let i = 0; i < 26; i++) {
76+
if (arr1[i] !== arr2[i]) {
77+
return false;
78+
}
79+
}
80+
return true;
81+
};
82+
83+
const arr1 = new Array(26).fill(0);
84+
for (const s of s1) {
85+
const index = toCode(s);
86+
arr1[index]++;
87+
}
88+
89+
const arr2 = new Array(26).fill(0);
90+
for (let i = 0; i < n; i++) {
91+
const index = toCode(s2[i]);
92+
arr2[index]++;
93+
}
94+
95+
for (let l = 0, r = n; r < m; l++, r++) {
96+
if (isMatch()) {
97+
return true;
98+
}
99+
100+
const i = toCode(s2[l]);
101+
const j = toCode(s2[r]);
102+
arr2[i]--;
103+
arr2[j]++;
104+
}
105+
return isMatch();
106+
}
107+
```
108+
109+
### **Rust**
110+
111+
```rust
112+
use std::collections::HashMap;
113+
114+
impl Solution {
115+
// 测试两个哈希表是否匹配
116+
fn is_match(m1: &HashMap<char, i32>, m2: &HashMap<char, i32>) -> bool {
117+
for (k, v) in m1.iter() {
118+
if m2.get(k).unwrap_or(&0) != v {
119+
return false;
120+
}
121+
}
122+
true
123+
}
124+
pub fn check_inclusion(s1: String, s2: String) -> bool {
125+
if s1.len() > s2.len() {
126+
return false;
127+
}
128+
let mut m1 = HashMap::new();
129+
let mut m2 = HashMap::new();
130+
// 初始化表 1
131+
for c in s1.chars() {
132+
m1.insert(c, m1.get(&c).unwrap_or(&0) + 1);
133+
}
134+
let cs: Vec<char> = s2.chars().collect();
135+
// 初始化窗口
136+
let mut i = 0;
137+
while i < s1.len() {
138+
m2.insert(cs[i], m2.get(&cs[i]).unwrap_or(&0) + 1);
139+
i += 1;
140+
}
141+
if Self::is_match(&m1, &m2) {
142+
return true;
143+
}
144+
// 持续滑动窗口,直到匹配或超出边界
145+
let mut j = 0;
146+
while i < cs.len() {
147+
m2.insert(cs[j], m2.get(&cs[j]).unwrap_or(&1) - 1);
148+
m2.insert(cs[i], m2.get(&cs[i]).unwrap_or(&0) + 1);
149+
j += 1;
150+
i += 1;
151+
if Self::is_match(&m1, &m2) {
152+
return true;
153+
}
154+
}
155+
false
156+
}
157+
}
158+
```
159+
61160
### **...**
62161

63162
```

‎solution/0500-0599/0567.Permutation in String/README_EN.md

+94
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,100 @@
4747

4848
```
4949

50+
### **TypeScript**
51+
52+
```ts
53+
function checkInclusion(s1: string, s2: string): boolean {
54+
if (s1.length > s2.length) {
55+
return false;
56+
}
57+
58+
const n = s1.length;
59+
const m = s2.length;
60+
61+
const toCode = (s: string) => s.charCodeAt(0) - 97;
62+
const isMatch = () => {
63+
for (let i = 0; i < 26; i++) {
64+
if (arr1[i] !== arr2[i]) {
65+
return false;
66+
}
67+
}
68+
return true;
69+
};
70+
71+
const arr1 = new Array(26).fill(0);
72+
for (const s of s1) {
73+
const index = toCode(s);
74+
arr1[index]++;
75+
}
76+
77+
const arr2 = new Array(26).fill(0);
78+
for (let i = 0; i < n; i++) {
79+
const index = toCode(s2[i]);
80+
arr2[index]++;
81+
}
82+
83+
for (let l = 0, r = n; r < m; l++, r++) {
84+
if (isMatch()) {
85+
return true;
86+
}
87+
88+
const i = toCode(s2[l]);
89+
const j = toCode(s2[r]);
90+
arr2[i]--;
91+
arr2[j]++;
92+
}
93+
return isMatch();
94+
}
95+
```
96+
97+
### **Rust**
98+
99+
```rust
100+
use std::collections::HashMap;
101+
102+
impl Solution {
103+
fn is_match(m1: &HashMap<char, i32>, m2: &HashMap<char, i32>) -> bool {
104+
for (k, v) in m1.iter() {
105+
if m2.get(k).unwrap_or(&0) != v {
106+
return false;
107+
}
108+
}
109+
true
110+
}
111+
pub fn check_inclusion(s1: String, s2: String) -> bool {
112+
if s1.len() > s2.len() {
113+
return false;
114+
}
115+
let mut m1 = HashMap::new();
116+
let mut m2 = HashMap::new();
117+
for c in s1.chars() {
118+
m1.insert(c, m1.get(&c).unwrap_or(&0) + 1);
119+
}
120+
let cs: Vec<char> = s2.chars().collect();
121+
let mut i = 0;
122+
while i < s1.len() {
123+
m2.insert(cs[i], m2.get(&cs[i]).unwrap_or(&0) + 1);
124+
i += 1;
125+
}
126+
if Self::is_match(&m1, &m2) {
127+
return true;
128+
}
129+
let mut j = 0;
130+
while i < cs.len() {
131+
m2.insert(cs[j], m2.get(&cs[j]).unwrap_or(&1) - 1);
132+
m2.insert(cs[i], m2.get(&cs[i]).unwrap_or(&0) + 1);
133+
j += 1;
134+
i += 1;
135+
if Self::is_match(&m1, &m2) {
136+
return true;
137+
}
138+
}
139+
false
140+
}
141+
}
142+
```
143+
50144
### **...**
51145

52146
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
// 测试两个哈希表是否匹配
5+
fn is_match(m1: &HashMap<char, i32>, m2: &HashMap<char, i32>) -> bool {
6+
for (k, v) in m1.iter() {
7+
if m2.get(k).unwrap_or(&0) != v {
8+
return false;
9+
}
10+
}
11+
true
12+
}
13+
pub fn check_inclusion(s1: String, s2: String) -> bool {
14+
if s1.len() > s2.len() {
15+
return false;
16+
}
17+
let mut m1 = HashMap::new();
18+
let mut m2 = HashMap::new();
19+
// 初始化表 1
20+
for c in s1.chars() {
21+
m1.insert(c, m1.get(&c).unwrap_or(&0) + 1);
22+
}
23+
let cs: Vec<char> = s2.chars().collect();
24+
// 初始化窗口
25+
let mut i = 0;
26+
while i < s1.len() {
27+
m2.insert(cs[i], m2.get(&cs[i]).unwrap_or(&0) + 1);
28+
i += 1;
29+
}
30+
if Self::is_match(&m1, &m2) {
31+
return true;
32+
}
33+
// 持续滑动窗口,直到匹配或超出边界
34+
let mut j = 0;
35+
while i < cs.len() {
36+
m2.insert(cs[j], m2.get(&cs[j]).unwrap_or(&1) - 1);
37+
m2.insert(cs[i], m2.get(&cs[i]).unwrap_or(&0) + 1);
38+
j += 1;
39+
i += 1;
40+
if Self::is_match(&m1, &m2) {
41+
return true;
42+
}
43+
}
44+
false
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function checkInclusion(s1: string, s2: string): boolean {
2+
// 滑动窗口方案
3+
if (s1.length > s2.length) {
4+
return false;
5+
}
6+
7+
const n = s1.length;
8+
const m = s2.length;
9+
10+
const toCode = (s: string) => s.charCodeAt(0) - 97;
11+
const isMatch = () => {
12+
for (let i = 0; i < 26; i++) {
13+
if (arr1[i] !== arr2[i]) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
};
19+
20+
const arr1 = new Array(26).fill(0);
21+
for (const s of s1) {
22+
const index = toCode(s);
23+
arr1[index]++;
24+
}
25+
26+
const arr2 = new Array(26).fill(0);
27+
for (let i = 0; i < n; i++) {
28+
const index = toCode(s2[i]);
29+
arr2[index]++;
30+
}
31+
32+
for (let l = 0, r = n; r < m; l++, r++) {
33+
if (isMatch()) {
34+
return true;
35+
}
36+
37+
const i = toCode(s2[l]);
38+
const j = toCode(s2[r]);
39+
arr2[i]--;
40+
arr2[j]++;
41+
}
42+
return isMatch();
43+
}

0 commit comments

Comments
 (0)
Please sign in to comment.