Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0567 (#3949)
Browse files Browse the repository at this point in the history
No.0567.Permutation in String
  • Loading branch information
yanglbme authored Jan 14, 2025
1 parent 355400e commit ba54758
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 970 deletions.
510 changes: 177 additions & 333 deletions solution/0500-0599/0567.Permutation in String/README.md

Large diffs are not rendered by default.

508 changes: 182 additions & 326 deletions solution/0500-0599/0567.Permutation in String/README_EN.md

Large diffs are not rendered by default.

34 changes: 19 additions & 15 deletions solution/0500-0599/0567.Permutation in String/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int n = s1.size(), m = s2.size();
if (n > m) {
return false;
int need = 0;
int cnt[26]{};
for (char c : s1) {
if (++cnt[c - 'a'] == 1) {
++need;
}
}
vector<int> cnt1(26), cnt2(26);
int m = s1.size(), n = s2.size();
for (int i = 0; i < n; ++i) {
++cnt1[s1[i] - 'a'];
++cnt2[s2[i] - 'a'];
}
if (cnt1 == cnt2) {
return true;
}
for (int i = n; i < m; ++i) {
++cnt2[s2[i] - 'a'];
--cnt2[s2[i - n] - 'a'];
if (cnt1 == cnt2) {
int c = s2[i] - 'a';
if (--cnt[c] == 0) {
--need;
}
if (i >= m) {
c = s2[i - m] - 'a';
if (++cnt[c] == 1) {
++need;
}
}
if (need == 0) {
return true;
}
}
return false;
}
};
};
32 changes: 32 additions & 0 deletions solution/0500-0599/0567.Permutation in String/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Solution {
public bool CheckInclusion(string s1, string s2) {
int need = 0;
int[] cnt = new int[26];

foreach (char c in s1) {
if (++cnt[c - 'a'] == 1) {
need++;
}
}

int m = s1.Length, n = s2.Length;
for (int i = 0; i < n; i++) {
int c = s2[i] - 'a';
if (--cnt[c] == 0) {
need--;
}

if (i >= m) {
c = s2[i - m] - 'a';
if (++cnt[c] == 1) {
need++;
}
}

if (need == 0) {
return true;
}
}
return false;
}
}
39 changes: 22 additions & 17 deletions solution/0500-0599/0567.Permutation in String/Solution.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
func checkInclusion(s1 string, s2 string) bool {
n, m := len(s1), len(s2)
if n > m {
return false
}
cnt1 := [26]int{}
cnt2 := [26]int{}
for i := range s1 {
cnt1[s1[i]-'a']++
cnt2[s2[i]-'a']++
}
if cnt1 == cnt2 {
return true
need := 0
cnt := [26]int{}

for _, c := range s1 {
if cnt[c-'a']++; cnt[c-'a'] == 1 {
need++
}
}
for i := n; i < m; i++ {
cnt2[s2[i]-'a']++
cnt2[s2[i-n]-'a']--
if cnt1 == cnt2 {

m, n := len(s1), len(s2)
for i := 0; i < n; i++ {
c := s2[i] - 'a'
if cnt[c]--; cnt[c] == 0 {
need--
}
if i >= m {
c = s2[i-m] - 'a'
if cnt[c]++; cnt[c] == 1 {
need++
}
}
if need == 0 {
return true
}
}
return false
}
}
36 changes: 19 additions & 17 deletions solution/0500-0599/0567.Permutation in String/Solution.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
class Solution {
public boolean checkInclusion(String s1, String s2) {
int n = s1.length();
int m = s2.length();
if (n > m) {
return false;
int need = 0;
int[] cnt = new int[26];
for (char c : s1.toCharArray()) {
if (++cnt[c - 'a'] == 1) {
++need;
}
}
int[] cnt1 = new int[26];
int[] cnt2 = new int[26];
int m = s1.length(), n = s2.length();
for (int i = 0; i < n; ++i) {
++cnt1[s1.charAt(i) - 'a'];
++cnt2[s2.charAt(i) - 'a'];
}
if (Arrays.equals(cnt1, cnt2)) {
return true;
}
for (int i = n; i < m; ++i) {
++cnt2[s2.charAt(i) - 'a'];
--cnt2[s2.charAt(i - n) - 'a'];
if (Arrays.equals(cnt1, cnt2)) {
int c = s2.charAt(i) - 'a';
if (--cnt[c] == 0) {
--need;
}
if (i >= m) {
c = s2.charAt(i - m) - 'a';
if (++cnt[c] == 1) {
++need;
}
}
if (need == 0) {
return true;
}
}
return false;
}
}
}
21 changes: 12 additions & 9 deletions solution/0500-0599/0567.Permutation in String/Solution.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
n = len(s1)
cnt1 = Counter(s1)
cnt2 = Counter(s2[:n])
if cnt1 == cnt2:
return True
for i in range(n, len(s2)):
cnt2[s2[i]] += 1
cnt2[s2[i - n]] -= 1
if cnt1 == cnt2:
cnt = Counter(s1)
need = len(cnt)
m = len(s1)
for i, c in enumerate(s2):
cnt[c] -= 1
if cnt[c] == 0:
need -= 1
if i >= m:
cnt[s2[i - m]] += 1
if cnt[s2[i - m]] == 1:
need += 1
if need == 0:
return True
return False
66 changes: 30 additions & 36 deletions solution/0500-0599/0567.Permutation in String/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
use std::collections::HashMap;

impl Solution {
// 测试两个哈希表是否匹配
fn is_match(m1: &HashMap<char, i32>, m2: &HashMap<char, i32>) -> bool {
for (k, v) in m1.iter() {
if m2.get(k).unwrap_or(&0) != v {
return false;
}
}
true
}
pub fn check_inclusion(s1: String, s2: String) -> bool {
if s1.len() > s2.len() {
return false;
}
let mut m1 = HashMap::new();
let mut m2 = HashMap::new();
// 初始化表 1
let mut need = 0;
let mut cnt = vec![0; 26];

for c in s1.chars() {
m1.insert(c, m1.get(&c).unwrap_or(&0) + 1);
}
let cs: Vec<char> = s2.chars().collect();
// 初始化窗口
let mut i = 0;
while i < s1.len() {
m2.insert(cs[i], m2.get(&cs[i]).unwrap_or(&0) + 1);
i += 1;
}
if Self::is_match(&m1, &m2) {
return true;
let index = (c as u8 - b'a') as usize;
if cnt[index] == 0 {
need += 1;
}
cnt[index] += 1;
}
// 持续滑动窗口,直到匹配或超出边界
let mut j = 0;
while i < cs.len() {
m2.insert(cs[j], m2.get(&cs[j]).unwrap_or(&1) - 1);
m2.insert(cs[i], m2.get(&cs[i]).unwrap_or(&0) + 1);
j += 1;
i += 1;
if Self::is_match(&m1, &m2) {

let m = s1.len();
let n = s2.len();
let s2_bytes = s2.as_bytes();

for i in 0..n {
let c = (s2_bytes[i] - b'a') as usize;
cnt[c] -= 1;
if cnt[c] == 0 {
need -= 1;
}

if i >= m {
let c = (s2_bytes[i - m] - b'a') as usize;
cnt[c] += 1;
if cnt[c] == 1 {
need += 1;
}
}

if need == 0 {
return true;
}
}

false
}
}
53 changes: 19 additions & 34 deletions solution/0500-0599/0567.Permutation in String/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@
function checkInclusion(s1: string, s2: string): boolean {
// 滑动窗口方案
if (s1.length > s2.length) {
return false;
}

const n = s1.length;
const m = s2.length;

const toCode = (s: string) => s.charCodeAt(0) - 97;
const isMatch = () => {
for (let i = 0; i < 26; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
let need = 0;
const cnt: number[] = Array(26).fill(0);
const a = 'a'.charCodeAt(0);
for (const c of s1) {
if (++cnt[c.charCodeAt(0) - a] === 1) {
need++;
}
return true;
};

const arr1 = new Array(26).fill(0);
for (const s of s1) {
const index = toCode(s);
arr1[index]++;
}

const arr2 = new Array(26).fill(0);
const [m, n] = [s1.length, s2.length];
for (let i = 0; i < n; i++) {
const index = toCode(s2[i]);
arr2[index]++;
}

for (let l = 0, r = n; r < m; l++, r++) {
if (isMatch()) {
let c = s2.charCodeAt(i) - a;
if (--cnt[c] === 0) {
need--;
}
if (i >= m) {
c = s2.charCodeAt(i - m) - a;
if (++cnt[c] === 1) {
need++;
}
}
if (need === 0) {
return true;
}

const i = toCode(s2[l]);
const j = toCode(s2[r]);
arr2[i]--;
arr2[j]++;
}
return isMatch();
return false;
}
43 changes: 0 additions & 43 deletions solution/0500-0599/0567.Permutation in String/Solution2.cpp

This file was deleted.

Loading

0 comments on commit ba54758

Please sign in to comment.