Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d68d794

Browse files
feat: add solutions to lcof2 problems: No.014,015,016
1 parent 6e7fa57 commit d68d794

File tree

12 files changed

+439
-4
lines changed

12 files changed

+439
-4
lines changed

‎lcof2/剑指 Offer II 014. 字符串中的变位词/README.md‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,104 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
维护一个长度固定的窗口向前滑动
47+
4648
<!-- tabs:start -->
4749

4850
### **Python3**
4951

5052
<!-- 这里可写当前语言的特殊实现逻辑 -->
5153

5254
```python
53-
55+
class Solution:
56+
def checkInclusion(self, s1: str, s2: str) -> bool:
57+
n1, n2 = len(s1), len(s2)
58+
if n1 > n2:
59+
return False
60+
window = [0 for _ in range(26)]
61+
for i in range(n1):
62+
window[ord(s1[i]) - ord('a')] += 1
63+
window[ord(s2[i]) - ord('a')] -= 1
64+
if self.check(window): return True
65+
for i in range(n1, n2):
66+
window[ord(s2[i]) - ord('a')] -= 1
67+
window[ord(s2[i - n1]) - ord('a')] += 1
68+
if self.check(window): return True
69+
return False
70+
71+
def check(self, window: List[int]) -> bool:
72+
return all([cnt == 0 for cnt in window])
5473
```
5574

5675
### **Java**
5776

5877
<!-- 这里可写当前语言的特殊实现逻辑 -->
5978

6079
```java
80+
class Solution {
81+
public boolean checkInclusion(String s1, String s2) {
82+
int n1 = s1.length(), n2 = s2.length();
83+
if (n1 > n2) {
84+
return false;
85+
}
86+
int[] window = new int[26];
87+
for (int i = 0; i < n1; i++) {
88+
window[s1.charAt(i) - 'a']++;
89+
window[s2.charAt(i) - 'a']--;
90+
}
91+
if (check(window)) {
92+
return true;
93+
}
94+
for (int i = n1; i < n2; i++) {
95+
window[s2.charAt(i) - 'a']--;
96+
window[s2.charAt(i - n1) - 'a']++;
97+
if (check(window)) {
98+
return true;
99+
}
100+
}
101+
return false;
102+
}
103+
104+
private boolean check(int[] window) {
105+
return Arrays.stream(window).allMatch(cnt -> cnt == 0);
106+
}
107+
}
108+
```
61109

110+
### **Go**
111+
112+
```go
113+
func checkInclusion(s1 string, s2 string) bool {
114+
n1, n2 := len(s1), len(s2)
115+
if n1 > n2 {
116+
return false
117+
}
118+
window := make([]int, 26)
119+
for i := 0; i < n1; i++ {
120+
window[s1[i]-'a']++
121+
window[s2[i]-'a']--
122+
}
123+
if check(window) {
124+
return true
125+
}
126+
for i := n1; i < n2; i++ {
127+
window[s2[i]-'a']--
128+
window[s2[i-n1]-'a']++
129+
if check(window) {
130+
return true
131+
}
132+
}
133+
return false
134+
}
135+
136+
func check(window []int) bool {
137+
for _, cnt := range window {
138+
if cnt != 0 {
139+
return false
140+
}
141+
}
142+
return true
143+
}
62144
```
63145

64146
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func checkInclusion(s1 string, s2 string) bool {
2+
n1, n2 := len(s1), len(s2)
3+
if n1 > n2 {
4+
return false
5+
}
6+
window := make([]int, 26)
7+
for i := 0; i < n1; i++ {
8+
window[s1[i]-'a']++
9+
window[s2[i]-'a']--
10+
}
11+
if check(window) {
12+
return true
13+
}
14+
for i := n1; i < n2; i++ {
15+
window[s2[i]-'a']--
16+
window[s2[i-n1]-'a']++
17+
if check(window) {
18+
return true
19+
}
20+
}
21+
return false
22+
}
23+
24+
func check(window []int) bool {
25+
for _, cnt := range window {
26+
if cnt != 0 {
27+
return false
28+
}
29+
}
30+
return true
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public boolean checkInclusion(String s1, String s2) {
3+
int n1 = s1.length(), n2 = s2.length();
4+
if (n1 > n2) {
5+
return false;
6+
}
7+
int[] window = new int[26];
8+
for (int i = 0; i < n1; i++) {
9+
window[s1.charAt(i) - 'a']++;
10+
window[s2.charAt(i) - 'a']--;
11+
}
12+
if (check(window)) {
13+
return true;
14+
}
15+
for (int i = n1; i < n2; i++) {
16+
window[s2.charAt(i) - 'a']--;
17+
window[s2.charAt(i - n1) - 'a']++;
18+
if (check(window)) {
19+
return true;
20+
}
21+
}
22+
return false;
23+
}
24+
25+
private boolean check(int[] window) {
26+
return Arrays.stream(window).allMatch(cnt -> cnt == 0);
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def checkInclusion(self, s1: str, s2: str) -> bool:
3+
n1, n2 = len(s1), len(s2)
4+
if n1 > n2:
5+
return False
6+
window = [0 for _ in range(26)]
7+
for i in range(n1):
8+
window[ord(s1[i]) - ord('a')] += 1
9+
window[ord(s2[i]) - ord('a')] -= 1
10+
if self.check(window): return True
11+
for i in range(n1, n2):
12+
window[ord(s2[i]) - ord('a')] -= 1
13+
window[ord(s2[i - n1]) - ord('a')] += 1
14+
if self.check(window): return True
15+
return False
16+
17+
def check(self, window: List[int]) -> bool:
18+
return all([cnt == 0 for cnt in window])

‎lcof2/剑指 Offer II 015. 字符串中的所有变位词/README.md‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,106 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
和上一题一样的思路,利用固定长度滑动窗口
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
5557

5658
<!-- 这里可写当前语言的特殊实现逻辑 -->
5759

5860
```python
59-
61+
class Solution:
62+
def findAnagrams(self, s: str, p: str) -> List[int]:
63+
m, n = len(s), len(p)
64+
if n > m:
65+
return []
66+
window, ans = [0 for _ in range(26)], []
67+
for i in range(n):
68+
window[ord(p[i]) - ord('a')] += 1
69+
window[ord(s[i]) - ord('a')] -= 1
70+
if self.check(window): ans.append(0)
71+
for i in range(n, m):
72+
window[ord(s[i]) - ord('a')] -= 1
73+
window[ord(s[i - n]) - ord('a')] += 1
74+
if self.check(window): ans.append(i - n + 1)
75+
return ans
76+
77+
def check(self, window: List[int]) -> bool:
78+
return all([cnt == 0 for cnt in window])
6079
```
6180

6281
### **Java**
6382

6483
<!-- 这里可写当前语言的特殊实现逻辑 -->
6584

6685
```java
86+
class Solution {
87+
public List<Integer> findAnagrams(String s, String p) {
88+
List<Integer> ans = new ArrayList<>();
89+
int m = s.length(), n = p.length();
90+
if (n > m) {
91+
return ans;
92+
}
93+
int[] window = new int[26];
94+
for (int i = 0; i < n; i++) {
95+
window[p.charAt(i) - 'a']++;
96+
window[s.charAt(i) - 'a']--;
97+
}
98+
if (check(window)) {
99+
ans.add(0);
100+
}
101+
for (int i = n; i < m; i++) {
102+
window[s.charAt(i) - 'a']--;
103+
window[s.charAt(i - n) - 'a']++;
104+
if (check(window)) {
105+
ans.add(i - n + 1);
106+
}
107+
}
108+
return ans;
109+
}
110+
111+
private boolean check(int[] window) {
112+
return Arrays.stream(window).allMatch(cnt -> cnt == 0);
113+
}
114+
}
115+
```
67116

117+
### **Go**
118+
119+
```go
120+
func findAnagrams(s string, p string) []int {
121+
m, n := len(s), len(p)
122+
if n > m {
123+
return []int{}
124+
}
125+
ans := make([]int, 0)
126+
window := make([]int, 26)
127+
for i := 0; i < n; i++ {
128+
window[p[i]-'a']++
129+
window[s[i]-'a']--
130+
}
131+
if check(window) {
132+
ans = append(ans, 0)
133+
}
134+
for i := n; i < m; i++ {
135+
window[s[i]-'a']--
136+
window[s[i-n]-'a']++
137+
if check(window) {
138+
ans = append(ans, i-n+1)
139+
}
140+
}
141+
return ans
142+
}
143+
144+
func check(window []int) bool {
145+
for _, cnt := range window {
146+
if cnt != 0 {
147+
return false
148+
}
149+
}
150+
return true
151+
}
68152
```
69153

70154
### **...**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func findAnagrams(s string, p string) []int {
2+
m, n := len(s), len(p)
3+
if n > m {
4+
return []int{}
5+
}
6+
ans := make([]int, 0)
7+
window := make([]int, 26)
8+
for i := 0; i < n; i++ {
9+
window[p[i]-'a']++
10+
window[s[i]-'a']--
11+
}
12+
if check(window) {
13+
ans = append(ans, 0)
14+
}
15+
for i := n; i < m; i++ {
16+
window[s[i]-'a']--
17+
window[s[i-n]-'a']++
18+
if check(window) {
19+
ans = append(ans, i-n+1)
20+
}
21+
}
22+
return ans
23+
}
24+
25+
func check(window []int) bool {
26+
for _, cnt := range window {
27+
if cnt != 0 {
28+
return false
29+
}
30+
}
31+
return true
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public List<Integer> findAnagrams(String s, String p) {
3+
List<Integer> ans = new ArrayList<>();
4+
int m = s.length(), n = p.length();
5+
if (n > m) {
6+
return ans;
7+
}
8+
int[] window = new int[26];
9+
for (int i = 0; i < n; i++) {
10+
window[p.charAt(i) - 'a']++;
11+
window[s.charAt(i) - 'a']--;
12+
}
13+
if (check(window)) {
14+
ans.add(0);
15+
}
16+
for (int i = n; i < m; i++) {
17+
window[s.charAt(i) - 'a']--;
18+
window[s.charAt(i - n) - 'a']++;
19+
if (check(window)) {
20+
ans.add(i - n + 1);
21+
}
22+
}
23+
return ans;
24+
}
25+
26+
private boolean check(int[] window) {
27+
return Arrays.stream(window).allMatch(cnt -> cnt == 0);
28+
}
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def findAnagrams(self, s: str, p: str) -> List[int]:
3+
m, n = len(s), len(p)
4+
if n > m:
5+
return []
6+
window, ans = [0 for _ in range(26)], []
7+
for i in range(n):
8+
window[ord(p[i]) - ord('a')] += 1
9+
window[ord(s[i]) - ord('a')] -= 1
10+
if self.check(window): ans.append(0)
11+
for i in range(n, m):
12+
window[ord(s[i]) - ord('a')] -= 1
13+
window[ord(s[i - n]) - ord('a')] += 1
14+
if self.check(window): ans.append(i - n + 1)
15+
return False
16+
17+
def check(self, window: List[int]) -> bool:
18+
return all([cnt == 0 for cnt in window])

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /