Skip to content

Commit da71a72

Browse files
authored
feat: add solutions to lcof2 problems: No.032,033 (#1402)
* No.032.有效的变位词 * No.033.变位词组
1 parent b4fcbf8 commit da71a72

File tree

12 files changed

+326
-158
lines changed

12 files changed

+326
-158
lines changed

lcof2/剑指 Offer II 032. 有效的变位词/README.md

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@
5050

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

53-
数组或哈希表累加 s 中每个字符出现的次数,再减去 t 中对应的每个字符出现的次数。遍历结束后,若字符中出现次数不为 0 的情况,返回 false,否则返回 true。
53+
**方法一:计数**
54+
55+
如果字符串 $s$ 与字符串 $t$ 长度不相等,或者字符串 $s$ 与字符串 $t$ 完全相等,那么 $s$ 和 $t$ 一定不是变位词,返回 `false`
56+
57+
否则,我们用一个长度为 $26$ 的数组 $cnt$ 维护字符串 $s$ 中每个字母出现的次数与字符串 $t$ 中每个字母出现的次数的差值。如果 $cnt$ 数组的所有元素都为 $0$,则 $s$ 和 $t$ 互为变位词,返回 `true`,否则返回 `false`
58+
59+
时间复杂度 $O(m + n + |\Sigma|)$,空间复杂度 $O(|\Sigma|)$,其中 $m$ 和 $n$ 分别是字符串 $s$ 和 $t$ 的长度,而 $|\Sigma|$ 是字符集,在本题中字符集为所有小写字母,因此 $|\Sigma| = 26$。
5460

5561
<!-- tabs:start -->
5662

@@ -63,15 +69,7 @@ class Solution:
6369
def isAnagram(self, s: str, t: str) -> bool:
6470
if len(s) != len(t) or s == t:
6571
return False
66-
n = len(s)
67-
chars = [0] * 26
68-
for i in range(n):
69-
chars[ord(s[i]) - ord('a')] += 1
70-
chars[ord(t[i]) - ord('a')] -= 1
71-
for c in chars:
72-
if c != 0:
73-
return False
74-
return True
72+
return Counter(s) == Counter(t)
7573
```
7674

7775
### **Java**
@@ -81,17 +79,18 @@ class Solution:
8179
```java
8280
class Solution {
8381
public boolean isAnagram(String s, String t) {
84-
int n;
85-
if ((n = s.length()) != t.length() || (Objects.equals(s, t))) {
82+
int m = s.length();
83+
int n = t.length();
84+
if (m != n || s.equals(t)) {
8685
return false;
8786
}
88-
int[] chars = new int[26];
89-
for (int i = 0; i < n; ++i) {
90-
++chars[s.charAt(i) - 'a'];
91-
--chars[t.charAt(i) - 'a'];
87+
int[] cnt = new int[26];
88+
for (int i = 0; i < m; ++i) {
89+
++cnt[s.charAt(i) - 'a'];
90+
--cnt[t.charAt(i) - 'a'];
9291
}
93-
for (int c : chars) {
94-
if (c != 0) {
92+
for (int x : cnt) {
93+
if (x != 0) {
9594
return false;
9695
}
9796
}
@@ -106,16 +105,20 @@ class Solution {
106105
class Solution {
107106
public:
108107
bool isAnagram(string s, string t) {
109-
if (s.size() != t.size() || s == t)
108+
int m = s.size();
109+
int n = t.size();
110+
if (m != n || s == t) {
110111
return false;
111-
vector<int> chars(26, 0);
112-
for (int i = 0, n = s.size(); i < n; ++i) {
113-
++chars[s[i] - 'a'];
114-
--chars[t[i] - 'a'];
115112
}
116-
for (int c : chars) {
117-
if (c != 0)
113+
vector<int> cnt(26);
114+
for (int i = 0; i < m; ++i) {
115+
++cnt[s[i] - 'a'];
116+
--cnt[t[i] - 'a'];
117+
}
118+
for (int x : cnt) {
119+
if (x) {
118120
return false;
121+
}
119122
}
120123
return true;
121124
}
@@ -126,23 +129,42 @@ public:
126129
127130
```go
128131
func isAnagram(s string, t string) bool {
129-
if len(s) != len(t) || s == t {
132+
m, n := len(s), len(t)
133+
if m != n || s == t {
130134
return false
131135
}
132-
var chars [26]int
133-
for i := 0; i < len(s); i++ {
134-
chars[s[i]-'a']++
135-
chars[t[i]-'a']--
136+
cnt := [26]int{}
137+
for i, c := range s {
138+
cnt[c-'a']++
139+
cnt[t[i]-'a']--
136140
}
137-
for _, c := range chars {
138-
if c != 0 {
141+
for _, x := range cnt {
142+
if x != 0 {
139143
return false
140144
}
141145
}
142146
return true
143147
}
144148
```
145149

150+
### **TypeScript**
151+
152+
```ts
153+
function isAnagram(s: string, t: string): boolean {
154+
const m = s.length;
155+
const n = t.length;
156+
if (m !== n || s === t) {
157+
return false;
158+
}
159+
const cnt: number[] = new Array(26).fill(0);
160+
for (let i = 0; i < m; ++i) {
161+
++cnt[s[i].charCodeAt(0) - 'a'.charCodeAt(0)];
162+
--cnt[t[i].charCodeAt(0) - 'a'.charCodeAt(0)];
163+
}
164+
return cnt.every(x => x === 0);
165+
}
166+
```
167+
146168
### **...**
147169

148170
```
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
class Solution {
2-
public:
3-
bool isAnagram(string s, string t) {
4-
if (s.size() != t.size() || s == t)
5-
return false;
6-
vector<int> chars(26, 0);
7-
for (int i = 0, n = s.size(); i < n; ++i) {
8-
++chars[s[i] - 'a'];
9-
--chars[t[i] - 'a'];
10-
}
11-
for (int c : chars) {
12-
if (c != 0)
13-
return false;
14-
}
15-
return true;
16-
}
1+
class Solution {
2+
public:
3+
bool isAnagram(string s, string t) {
4+
int m = s.size();
5+
int n = t.size();
6+
if (m != n || s == t) {
7+
return false;
8+
}
9+
vector<int> cnt(26);
10+
for (int i = 0; i < m; ++i) {
11+
++cnt[s[i] - 'a'];
12+
--cnt[t[i] - 'a'];
13+
}
14+
for (int x : cnt) {
15+
if (x) {
16+
return false;
17+
}
18+
}
19+
return true;
20+
}
1721
};

lcof2/剑指 Offer II 032. 有效的变位词/Solution.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
func isAnagram(s string, t string) bool {
2-
if len(s) != len(t) || s == t {
2+
m, n := len(s), len(t)
3+
if m != n || s == t {
34
return false
45
}
5-
var chars [26]int
6-
for i := 0; i < len(s); i++ {
7-
chars[s[i]-'a']++
8-
chars[t[i]-'a']--
6+
cnt := [26]int{}
7+
for i, c := range s {
8+
cnt[c-'a']++
9+
cnt[t[i]-'a']--
910
}
10-
for _, c := range chars {
11-
if c != 0 {
11+
for _, x := range cnt {
12+
if x != 0 {
1213
return false
1314
}
1415
}
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
class Solution {
2-
public boolean isAnagram(String s, String t) {
3-
int n;
4-
if ((n = s.length()) != t.length() || (Objects.equals(s, t))) {
5-
return false;
6-
}
7-
int[] chars = new int[26];
8-
for (int i = 0; i < n; ++i) {
9-
++chars[s.charAt(i) - 'a'];
10-
--chars[t.charAt(i) - 'a'];
11-
}
12-
for (int c : chars) {
13-
if (c != 0) {
14-
return false;
15-
}
16-
}
17-
return true;
18-
}
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
int m = s.length();
4+
int n = t.length();
5+
if (m != n || s.equals(t)) {
6+
return false;
7+
}
8+
int[] cnt = new int[26];
9+
for (int i = 0; i < m; ++i) {
10+
++cnt[s.charAt(i) - 'a'];
11+
--cnt[t.charAt(i) - 'a'];
12+
}
13+
for (int x : cnt) {
14+
if (x != 0) {
15+
return false;
16+
}
17+
}
18+
return true;
19+
}
1920
}
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
class Solution:
2-
def isAnagram(self, s: str, t: str) -> bool:
3-
if len(s) != len(t) or s == t:
4-
return False
5-
n = len(s)
6-
chars = [0] * 26
7-
for i in range(n):
8-
chars[ord(s[i]) - ord('a')] += 1
9-
chars[ord(t[i]) - ord('a')] -= 1
10-
for c in chars:
11-
if c != 0:
12-
return False
13-
return True
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
if len(s) != len(t) or s == t:
4+
return False
5+
return Counter(s) == Counter(t)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
const m = s.length;
3+
const n = t.length;
4+
if (m !== n || s === t) {
5+
return false;
6+
}
7+
const cnt: number[] = new Array(26).fill(0);
8+
for (let i = 0; i < m; ++i) {
9+
++cnt[s[i].charCodeAt(0) - 'a'.charCodeAt(0)];
10+
--cnt[t[i].charCodeAt(0) - 'a'.charCodeAt(0)];
11+
}
12+
return cnt.every(x => x === 0);
13+
}

0 commit comments

Comments
 (0)