Skip to content

Commit ba54758

Browse files
authored
feat: add solutions to lc problem: No.0567 (#3949)
No.0567.Permutation in String
1 parent 355400e commit ba54758

14 files changed

+512
-970
lines changed

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

+177-333
Large diffs are not rendered by default.

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

+182-326
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
class Solution {
22
public:
33
bool checkInclusion(string s1, string s2) {
4-
int n = s1.size(), m = s2.size();
5-
if (n > m) {
6-
return false;
4+
int need = 0;
5+
int cnt[26]{};
6+
for (char c : s1) {
7+
if (++cnt[c - 'a'] == 1) {
8+
++need;
9+
}
710
}
8-
vector<int> cnt1(26), cnt2(26);
11+
int m = s1.size(), n = s2.size();
912
for (int i = 0; i < n; ++i) {
10-
++cnt1[s1[i] - 'a'];
11-
++cnt2[s2[i] - 'a'];
12-
}
13-
if (cnt1 == cnt2) {
14-
return true;
15-
}
16-
for (int i = n; i < m; ++i) {
17-
++cnt2[s2[i] - 'a'];
18-
--cnt2[s2[i - n] - 'a'];
19-
if (cnt1 == cnt2) {
13+
int c = s2[i] - 'a';
14+
if (--cnt[c] == 0) {
15+
--need;
16+
}
17+
if (i >= m) {
18+
c = s2[i - m] - 'a';
19+
if (++cnt[c] == 1) {
20+
++need;
21+
}
22+
}
23+
if (need == 0) {
2024
return true;
2125
}
2226
}
2327
return false;
2428
}
25-
};
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Solution {
2+
public bool CheckInclusion(string s1, string s2) {
3+
int need = 0;
4+
int[] cnt = new int[26];
5+
6+
foreach (char c in s1) {
7+
if (++cnt[c - 'a'] == 1) {
8+
need++;
9+
}
10+
}
11+
12+
int m = s1.Length, n = s2.Length;
13+
for (int i = 0; i < n; i++) {
14+
int c = s2[i] - 'a';
15+
if (--cnt[c] == 0) {
16+
need--;
17+
}
18+
19+
if (i >= m) {
20+
c = s2[i - m] - 'a';
21+
if (++cnt[c] == 1) {
22+
need++;
23+
}
24+
}
25+
26+
if (need == 0) {
27+
return true;
28+
}
29+
}
30+
return false;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
func checkInclusion(s1 string, s2 string) bool {
2-
n, m := len(s1), len(s2)
3-
if n > m {
4-
return false
5-
}
6-
cnt1 := [26]int{}
7-
cnt2 := [26]int{}
8-
for i := range s1 {
9-
cnt1[s1[i]-'a']++
10-
cnt2[s2[i]-'a']++
11-
}
12-
if cnt1 == cnt2 {
13-
return true
2+
need := 0
3+
cnt := [26]int{}
4+
5+
for _, c := range s1 {
6+
if cnt[c-'a']++; cnt[c-'a'] == 1 {
7+
need++
8+
}
149
}
15-
for i := n; i < m; i++ {
16-
cnt2[s2[i]-'a']++
17-
cnt2[s2[i-n]-'a']--
18-
if cnt1 == cnt2 {
10+
11+
m, n := len(s1), len(s2)
12+
for i := 0; i < n; i++ {
13+
c := s2[i] - 'a'
14+
if cnt[c]--; cnt[c] == 0 {
15+
need--
16+
}
17+
if i >= m {
18+
c = s2[i-m] - 'a'
19+
if cnt[c]++; cnt[c] == 1 {
20+
need++
21+
}
22+
}
23+
if need == 0 {
1924
return true
2025
}
2126
}
2227
return false
23-
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
class Solution {
22
public boolean checkInclusion(String s1, String s2) {
3-
int n = s1.length();
4-
int m = s2.length();
5-
if (n > m) {
6-
return false;
3+
int need = 0;
4+
int[] cnt = new int[26];
5+
for (char c : s1.toCharArray()) {
6+
if (++cnt[c - 'a'] == 1) {
7+
++need;
8+
}
79
}
8-
int[] cnt1 = new int[26];
9-
int[] cnt2 = new int[26];
10+
int m = s1.length(), n = s2.length();
1011
for (int i = 0; i < n; ++i) {
11-
++cnt1[s1.charAt(i) - 'a'];
12-
++cnt2[s2.charAt(i) - 'a'];
13-
}
14-
if (Arrays.equals(cnt1, cnt2)) {
15-
return true;
16-
}
17-
for (int i = n; i < m; ++i) {
18-
++cnt2[s2.charAt(i) - 'a'];
19-
--cnt2[s2.charAt(i - n) - 'a'];
20-
if (Arrays.equals(cnt1, cnt2)) {
12+
int c = s2.charAt(i) - 'a';
13+
if (--cnt[c] == 0) {
14+
--need;
15+
}
16+
if (i >= m) {
17+
c = s2.charAt(i - m) - 'a';
18+
if (++cnt[c] == 1) {
19+
++need;
20+
}
21+
}
22+
if (need == 0) {
2123
return true;
2224
}
2325
}
2426
return false;
2527
}
26-
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
class Solution:
22
def checkInclusion(self, s1: str, s2: str) -> bool:
3-
n = len(s1)
4-
cnt1 = Counter(s1)
5-
cnt2 = Counter(s2[:n])
6-
if cnt1 == cnt2:
7-
return True
8-
for i in range(n, len(s2)):
9-
cnt2[s2[i]] += 1
10-
cnt2[s2[i - n]] -= 1
11-
if cnt1 == cnt2:
3+
cnt = Counter(s1)
4+
need = len(cnt)
5+
m = len(s1)
6+
for i, c in enumerate(s2):
7+
cnt[c] -= 1
8+
if cnt[c] == 0:
9+
need -= 1
10+
if i >= m:
11+
cnt[s2[i - m]] += 1
12+
if cnt[s2[i - m]] == 1:
13+
need += 1
14+
if need == 0:
1215
return True
1316
return False
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,40 @@
1-
use std::collections::HashMap;
2-
31
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-
}
132
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
3+
let mut need = 0;
4+
let mut cnt = vec![0; 26];
5+
206
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;
7+
let index = (c as u8 - b'a') as usize;
8+
if cnt[index] == 0 {
9+
need += 1;
10+
}
11+
cnt[index] += 1;
3212
}
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) {
13+
14+
let m = s1.len();
15+
let n = s2.len();
16+
let s2_bytes = s2.as_bytes();
17+
18+
for i in 0..n {
19+
let c = (s2_bytes[i] - b'a') as usize;
20+
cnt[c] -= 1;
21+
if cnt[c] == 0 {
22+
need -= 1;
23+
}
24+
25+
if i >= m {
26+
let c = (s2_bytes[i - m] - b'a') as usize;
27+
cnt[c] += 1;
28+
if cnt[c] == 1 {
29+
need += 1;
30+
}
31+
}
32+
33+
if need == 0 {
4134
return true;
4235
}
4336
}
37+
4438
false
4539
}
4640
}
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,28 @@
11
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-
}
2+
let need = 0;
3+
const cnt: number[] = Array(26).fill(0);
4+
const a = 'a'.charCodeAt(0);
5+
for (const c of s1) {
6+
if (++cnt[c.charCodeAt(0) - a] === 1) {
7+
need++;
168
}
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]++;
249
}
2510

26-
const arr2 = new Array(26).fill(0);
11+
const [m, n] = [s1.length, s2.length];
2712
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()) {
13+
let c = s2.charCodeAt(i) - a;
14+
if (--cnt[c] === 0) {
15+
need--;
16+
}
17+
if (i >= m) {
18+
c = s2.charCodeAt(i - m) - a;
19+
if (++cnt[c] === 1) {
20+
need++;
21+
}
22+
}
23+
if (need === 0) {
3424
return true;
3525
}
36-
37-
const i = toCode(s2[l]);
38-
const j = toCode(s2[r]);
39-
arr2[i]--;
40-
arr2[j]++;
4126
}
42-
return isMatch();
27+
return false;
4328
}

solution/0500-0599/0567.Permutation in String/Solution2.cpp

-43
This file was deleted.

0 commit comments

Comments
 (0)