Skip to content

feat: add solutions to lcof2 problems: No.014,015 #1401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
394 changes: 308 additions & 86 deletions lcof2/剑指 Offer II 014. 字符串中的变位词/README.md

Large diffs are not rendered by default.

89 changes: 44 additions & 45 deletions lcof2/剑指 Offer II 014. 字符串中的变位词/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
class Solution {
public:
bool checkInclusion(string s1, string s2) {

int len1 = s1.size();
int len2 = s2.size();

if (len2 < len1) {
return false;
}

int count[30] = {0};

for (int i = 0; i < len1; ++i) {
++count[s1[i] - 'a'];
--count[s2[i] - 'a'];
}

int l = 0;
int r = len1 - 1;

while (r < len2) {

bool flag = true;

for (int i : count) {
if (i != 0) {
flag = false;
}
}

if (flag) {
return true;
}

if (r + 1 >= len2) {
break;
}

++count[s2[l++] - 'a'];
--count[s2[++r] - 'a'];
}

return false;
}
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int m = s1.size(), n = s2.size();
if (m > n) {
return false;
}
vector<int> cnt(26);
for (int i = 0; i < m; ++i) {
--cnt[s1[i] - 'a'];
++cnt[s2[i] - 'a'];
}
int diff = 0;
for (int x : cnt) {
if (x != 0) {
++diff;
}
}
if (diff == 0) {
return true;
}
for (int i = m; i < n; ++i) {
int a = s2[i - m] - 'a';
int b = s2[i] - 'a';
if (cnt[a] == 0) {
++diff;
}
--cnt[a];
if (cnt[a] == 0) {
--diff;
}
if (cnt[b] == 0) {
++diff;
}
++cnt[b];
if (cnt[b] == 0) {
--diff;
}
if (diff == 0) {
return true;
}
}
return false;
}
};
50 changes: 30 additions & 20 deletions lcof2/剑指 Offer II 014. 字符串中的变位词/Solution.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
func checkInclusion(s1 string, s2 string) bool {
n1, n2 := len(s1), len(s2)
if n1 > n2 {
m, n := len(s1), len(s2)
if m > n {
return false
}
window := make([]int, 26)
for i := 0; i < n1; i++ {
window[s1[i]-'a']++
window[s2[i]-'a']--
cnt := [26]int{}
for i := 0; i < m; i++ {
cnt[s1[i]-'a']--
cnt[s2[i]-'a']++
}
if check(window) {
diff := 0
for _, x := range cnt {
if x != 0 {
diff++
}
}
if diff == 0 {
return true
}
for i := n1; i < n2; i++ {
window[s2[i]-'a']--
window[s2[i-n1]-'a']++
if check(window) {
for i := m; i < n; i++ {
a, b := s2[i-m]-'a', s2[i]-'a'
if cnt[a] == 0 {
diff++
}
cnt[a]--
if cnt[a] == 0 {
diff--
}
if cnt[b] == 0 {
diff++
}
cnt[b]++
if cnt[b] == 0 {
diff--
}
if diff == 0 {
return true
}
}
return false
}

func check(window []int) bool {
for _, cnt := range window {
if cnt != 0 {
return false
}
}
return true
}
73 changes: 45 additions & 28 deletions lcof2/剑指 Offer II 014. 字符串中的变位词/Solution.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
class Solution {
public boolean checkInclusion(String s1, String s2) {
int n1 = s1.length(), n2 = s2.length();
if (n1 > n2) {
return false;
}
int[] window = new int[26];
for (int i = 0; i < n1; i++) {
window[s1.charAt(i) - 'a']++;
window[s2.charAt(i) - 'a']--;
}
if (check(window)) {
return true;
}
for (int i = n1; i < n2; i++) {
window[s2.charAt(i) - 'a']--;
window[s2.charAt(i - n1) - 'a']++;
if (check(window)) {
return true;
}
}
return false;
}

private boolean check(int[] window) {
return Arrays.stream(window).allMatch(cnt -> cnt == 0);
}
}
class Solution {
public boolean checkInclusion(String s1, String s2) {
int m = s1.length();
int n = s2.length();
if (m > n) {
return false;
}
int[] cnt = new int[26];
for (int i = 0; i < m; ++i) {
--cnt[s1.charAt(i) - 'a'];
++cnt[s2.charAt(i) - 'a'];
}
int diff = 0;
for (int x : cnt) {
if (x != 0) {
++diff;
}
}
if (diff == 0) {
return true;
}
for (int i = m; i < n; ++i) {
int a = s2.charAt(i - m) - 'a';
int b = s2.charAt(i) - 'a';
if (cnt[a] == 0) {
++diff;
}
--cnt[a];
if (cnt[a] == 0) {
--diff;
}
if (cnt[b] == 0) {
++diff;
}
++cnt[b];
if (cnt[b] == 0) {
--diff;
}
if (diff == 0) {
return true;
}
}
return false;
}
}
47 changes: 27 additions & 20 deletions lcof2/剑指 Offer II 014. 字符串中的变位词/Solution.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
n1, n2 = len(s1), len(s2)
if n1 > n2:
return False
window = [0 for _ in range(26)]
for i in range(n1):
window[ord(s1[i]) - ord('a')] += 1
window[ord(s2[i]) - ord('a')] -= 1
if self.check(window):
return True
for i in range(n1, n2):
window[ord(s2[i]) - ord('a')] -= 1
window[ord(s2[i - n1]) - ord('a')] += 1
if self.check(window):
return True
return False

def check(self, window: List[int]) -> bool:
return all([cnt == 0 for cnt in window])
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
m, n = len(s1), len(s2)
if m > n:
return False
cnt = Counter()
for a, b in zip(s1, s2):
cnt[a] -= 1
cnt[b] += 1
diff = sum(x != 0 for x in cnt.values())
if diff == 0:
return True
for i in range(m, n):
a, b = s2[i - m], s2[i]
if cnt[a] == 0:
diff += 1
cnt[a] -= 1
if cnt[a] == 0:
diff -= 1
if cnt[b] == 0:
diff += 1
cnt[b] += 1
if cnt[b] == 0:
diff -= 1
if diff == 0:
return True
return False
41 changes: 41 additions & 0 deletions lcof2/剑指 Offer II 014. 字符串中的变位词/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function checkInclusion(s1: string, s2: string): boolean {
const m = s1.length;
const n = s2.length;
if (m > n) {
return false;
}
const cnt: number[] = new Array(26).fill(0);
for (let i = 0; i < m; ++i) {
--cnt[s1[i].charCodeAt(0) - 'a'.charCodeAt(0)];
++cnt[s2[i].charCodeAt(0) - 'a'.charCodeAt(0)];
}
let diff = 0;
for (const x of cnt) {
if (x !== 0) {
++diff;
}
}
if (diff === 0) {
return true;
}
for (let i = m; i < n; ++i) {
const a = s2[i - m].charCodeAt(0) - 'a'.charCodeAt(0);
const b = s2[i].charCodeAt(0) - 'a'.charCodeAt(0);
if (cnt[a] === 0) {
++diff;
}
if (--cnt[a] === 0) {
--diff;
}
if (cnt[b] === 0) {
++diff;
}
if (++cnt[b] === 0) {
--diff;
}
if (diff === 0) {
return true;
}
}
return false;
}
Loading