Skip to content

Commit 760635a

Browse files
committedMar 26, 2021
feat: update lcci solutions: 01.01 & 01.02
1 parent 33d0229 commit 760635a

File tree

8 files changed

+121
-49
lines changed

8 files changed

+121
-49
lines changed
 

‎lcci/01.01.Is Unique/README.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

3030
<!-- 这里可写通用的实现逻辑 -->
3131

32+
根据示例,可以假定字符串中只包含小写字母(实际验证,也符合假设)。
33+
34+
用 bitmap 标记小写字母是否出现过。
35+
3236
<!-- tabs:start -->
3337

3438
### **Python3**
@@ -38,8 +42,13 @@
3842
```python
3943
class Solution:
4044
def isUnique(self, astr: str) -> bool:
41-
sets = set(astr)
42-
return len(sets) == len(astr)
45+
bitmap = 0
46+
for c in astr:
47+
pos = ord(c) - ord('a')
48+
if (bitmap & (1 << pos)) != 0:
49+
return False
50+
bitmap |= (1 << pos)
51+
return True
4352
```
4453

4554
### **Java**
@@ -49,14 +58,13 @@ class Solution:
4958
```java
5059
class Solution {
5160
public boolean isUnique(String astr) {
52-
char[] chars = astr.toCharArray();
53-
int len = chars.length;
54-
for (int i = 0; i < len - 1; ++i) {
55-
for (int j = i + 1; j < len; ++j) {
56-
if (chars[i] == chars[j]) {
57-
return false;
58-
}
61+
int bitmap = 0;
62+
for (int i = 0, n = astr.length(); i < n; ++i) {
63+
int pos = astr.charAt(i) - 'a';
64+
if ((bitmap & (1 << pos)) != 0) {
65+
return false;
5966
}
67+
bitmap |= (1 << pos);
6068
}
6169
return true;
6270
}

‎lcci/01.01.Is Unique/README_EN.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,27 @@
4141
```python
4242
class Solution:
4343
def isUnique(self, astr: str) -> bool:
44-
sets = set(astr)
45-
return len(sets) == len(astr)
44+
bitmap = 0
45+
for c in astr:
46+
pos = ord(c) - ord('a')
47+
if (bitmap & (1 << pos)) != 0:
48+
return False
49+
bitmap |= (1 << pos)
50+
return True
4651
```
4752

4853
### **Java**
4954

5055
```java
5156
class Solution {
5257
public boolean isUnique(String astr) {
53-
char[] chars = astr.toCharArray();
54-
int len = chars.length;
55-
for (int i = 0; i < len - 1; ++i) {
56-
for (int j = i + 1; j < len; ++j) {
57-
if (chars[i] == chars[j]) {
58-
return false;
59-
}
58+
int bitmap = 0;
59+
for (int i = 0, n = astr.length(); i < n; ++i) {
60+
int pos = astr.charAt(i) - 'a';
61+
if ((bitmap & (1 << pos)) != 0) {
62+
return false;
6063
}
64+
bitmap |= (1 << pos);
6165
}
6266
return true;
6367
}

‎lcci/01.01.Is Unique/Solution.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class Solution {
22
public boolean isUnique(String astr) {
3-
char[] chars = astr.toCharArray();
4-
int len = chars.length;
5-
for (int i = 0; i < len - 1; ++i) {
6-
for (int j = i + 1; j < len; ++j) {
7-
if (chars[i] == chars[j]) {
8-
return false;
9-
}
3+
int bitmap = 0;
4+
for (int i = 0, n = astr.length(); i < n; ++i) {
5+
int pos = astr.charAt(i) - 'a';
6+
if ((bitmap & (1 << pos)) != 0) {
7+
return false;
108
}
9+
bitmap |= (1 << pos);
1110
}
1211
return true;
1312
}

‎lcci/01.01.Is Unique/Solution.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
class Solution:
22
def isUnique(self, astr: str) -> bool:
3-
sets = set(astr)
4-
return len(sets) == len(astr)
3+
bitmap = 0
4+
for c in astr:
5+
pos = ord(c) - ord('a')
6+
if (bitmap & (1 << pos)) != 0:
7+
return False
8+
bitmap |= (1 << pos)
9+
return True

‎lcci/01.02.Check Permutation/README.md

+28-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
<!-- 这里可写通用的实现逻辑 -->
3232

33+
用一个哈希表作为字符计数器,`O(n)` 时间内解决。
34+
3335
<!-- tabs:start -->
3436

3537
### **Python3**
@@ -39,7 +41,17 @@
3941
```python
4042
class Solution:
4143
def CheckPermutation(self, s1: str, s2: str) -> bool:
42-
return sorted(s1) == sorted(s2)
44+
n1, n2 = len(s1), len(s2)
45+
if n1 != n2:
46+
return False
47+
counter = collections.Counter()
48+
for i in range(n1):
49+
counter[s1[i]] += 1
50+
counter[s2[i]] -= 1
51+
for val in counter.values():
52+
if val != 0:
53+
return False
54+
return True
4355
```
4456

4557
### **Java**
@@ -48,15 +60,23 @@ class Solution:
4860

4961
```java
5062
class Solution {
51-
public boolean checkPermutation(String s1, String s2) {
52-
if (s1 == null || s2 == null || s1.length() != s2.length()) {
63+
public boolean CheckPermutation(String s1, String s2) {
64+
int n1 = s1.length(), n2 = s2.length();
65+
if (n1 != n2) {
5366
return false;
5467
}
55-
char[] c1 = s1.toCharArray();
56-
char[] c2 = s2.toCharArray();
57-
Arrays.sort(c1);
58-
Arrays.sort(c2);
59-
return Arrays.equals(c1, c2);
68+
Map<Character, Integer> counter = new HashMap<>();
69+
for (int i = 0; i < n1; ++i) {
70+
char c1 = s1.charAt(i), c2 = s2.charAt(i);
71+
counter.put(c1, counter.getOrDefault(c1, 0) + 1);
72+
counter.put(c2, counter.getOrDefault(c2, 0) - 1);
73+
}
74+
for (int val : counter.values()) {
75+
if (val != 0) {
76+
return false;
77+
}
78+
}
79+
return true;
6080
}
6181
}
6282
```

‎lcci/01.02.Check Permutation/README_EN.md

+25-7
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,40 @@
4141
```python
4242
class Solution:
4343
def CheckPermutation(self, s1: str, s2: str) -> bool:
44-
return sorted(s1) == sorted(s2)
44+
n1, n2 = len(s1), len(s2)
45+
if n1 != n2:
46+
return False
47+
counter = collections.Counter()
48+
for i in range(n1):
49+
counter[s1[i]] += 1
50+
counter[s2[i]] -= 1
51+
for val in counter.values():
52+
if val != 0:
53+
return False
54+
return True
4555
```
4656

4757
### **Java**
4858

4959
```java
5060
class Solution {
5161
public boolean CheckPermutation(String s1, String s2) {
52-
if (s1 == null || s2 == null || s1.length() != s2.length()) {
62+
int n1 = s1.length(), n2 = s2.length();
63+
if (n1 != n2) {
5364
return false;
5465
}
55-
char[] c1 = s1.toCharArray();
56-
char[] c2 = s2.toCharArray();
57-
Arrays.sort(c1);
58-
Arrays.sort(c2);
59-
return Arrays.equals(c1, c2);
66+
Map<Character, Integer> counter = new HashMap<>();
67+
for (int i = 0; i < n1; ++i) {
68+
char c1 = s1.charAt(i), c2 = s2.charAt(i);
69+
counter.put(c1, counter.getOrDefault(c1, 0) + 1);
70+
counter.put(c2, counter.getOrDefault(c2, 0) - 1);
71+
}
72+
for (int val : counter.values()) {
73+
if (val != 0) {
74+
return false;
75+
}
76+
}
77+
return true;
6078
}
6179
}
6280
```
+14-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
class Solution {
22
public boolean CheckPermutation(String s1, String s2) {
3-
if (s1 == null || s2 == null || s1.length() != s2.length()) {
3+
int n1 = s1.length(), n2 = s2.length();
4+
if (n1 != n2) {
45
return false;
56
}
6-
char[] c1 = s1.toCharArray();
7-
char[] c2 = s2.toCharArray();
8-
Arrays.sort(c1);
9-
Arrays.sort(c2);
10-
return Arrays.equals(c1, c2);
7+
Map<Character, Integer> counter = new HashMap<>();
8+
for (int i = 0; i < n1; ++i) {
9+
char c1 = s1.charAt(i), c2 = s2.charAt(i);
10+
counter.put(c1, counter.getOrDefault(c1, 0) + 1);
11+
counter.put(c2, counter.getOrDefault(c2, 0) - 1);
12+
}
13+
for (int val : counter.values()) {
14+
if (val != 0) {
15+
return false;
16+
}
17+
}
18+
return true;
1119
}
1220
}
+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
class Solution:
22
def CheckPermutation(self, s1: str, s2: str) -> bool:
3-
return sorted(s1) == sorted(s2)
3+
n1, n2 = len(s1), len(s2)
4+
if n1 != n2:
5+
return False
6+
counter = collections.Counter()
7+
for i in range(n1):
8+
counter[s1[i]] += 1
9+
counter[s2[i]] -= 1
10+
for val in counter.values():
11+
if val != 0:
12+
return False
13+
return True

0 commit comments

Comments
 (0)
Please sign in to comment.