Skip to content

Commit b389888

Browse files
authored
feat: add solutions to lcci problems: No.01.02,01.04 (#4065)
1 parent 24c25fd commit b389888

23 files changed

+458
-221
lines changed

lcci/01.01.Is Unique/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/01.01.Is%20Unique/REA
1919
<p><strong>示例 1:</strong></p>
2020

2121
<pre><strong>输入:</strong> s = &quot;leetcode&quot;
22-
<strong>输出:</strong> false
22+
<strong>输出:</strong> false
2323
</pre>
2424

2525
<p><strong>示例 2:</strong></p>
@@ -56,8 +56,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/01.01.Is%20Unique/REA
5656
class Solution:
5757
def isUnique(self, astr: str) -> bool:
5858
mask = 0
59-
for c in astr:
60-
i = ord(c) - ord('a')
59+
for i in map(lambda c: ord(c) - ord("a"), astr):
6160
if (mask >> i) & 1:
6261
return False
6362
mask |= 1 << i

lcci/01.01.Is Unique/README_EN.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ The time complexity is $O(n)$, where $n$ is the length of the string. The space
6464
class Solution:
6565
def isUnique(self, astr: str) -> bool:
6666
mask = 0
67-
for c in astr:
68-
i = ord(c) - ord('a')
67+
for i in map(lambda c: ord(c) - ord("a"), astr):
6968
if (mask >> i) & 1:
7069
return False
7170
mask |= 1 << i

lcci/01.01.Is Unique/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def isUnique(self, astr: str) -> bool:
33
mask = 0
4-
for c in astr:
5-
i = ord(c) - ord('a')
4+
for i in map(lambda c: ord(c) - ord("a"), astr):
65
if (mask >> i) & 1:
76
return False
87
mask |= 1 << i

lcci/01.02.Check Permutation/README.md

+73-42
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,18 @@ class Solution {
9393
class Solution {
9494
public:
9595
bool CheckPermutation(string s1, string s2) {
96-
if (s1.size() != s2.size()) return false;
97-
int cnt[26] = {0};
98-
for (char& c : s1) ++cnt[c - 'a'];
99-
for (char& c : s2)
100-
if (--cnt[c - 'a'] < 0) return false;
96+
if (s1.size() != s2.size()) {
97+
return false;
98+
}
99+
int cnt[26]{};
100+
for (char c : s1) {
101+
++cnt[c - 'a'];
102+
}
103+
for (char c : s2) {
104+
if (--cnt[c - 'a'] < 0) {
105+
return false;
106+
}
107+
}
101108
return true;
102109
}
103110
};
@@ -115,8 +122,7 @@ func CheckPermutation(s1 string, s2 string) bool {
115122
cnt[c-'a']++
116123
}
117124
for _, c := range s2 {
118-
cnt[c-'a']--
119-
if cnt[c-'a'] < 0 {
125+
if cnt[c-'a']--; cnt[c-'a'] < 0 {
120126
return false
121127
}
122128
}
@@ -128,20 +134,18 @@ func CheckPermutation(s1 string, s2 string) bool {
128134

129135
```ts
130136
function CheckPermutation(s1: string, s2: string): boolean {
131-
const n = s1.length;
132-
const m = s2.length;
133-
if (n !== m) {
137+
if (s1.length !== s2.length) {
134138
return false;
135139
}
136-
const map = new Map<string, number>();
137-
for (let i = 0; i < n; i++) {
138-
map.set(s1[i], (map.get(s1[i]) ?? 0) + 1);
139-
map.set(s2[i], (map.get(s2[i]) ?? 0) - 1);
140+
const cnt: Record<string, number> = {};
141+
for (const c of s1) {
142+
cnt[c] = (cnt[c] || 0) + 1;
140143
}
141-
for (const v of map.values()) {
142-
if (v !== 0) {
144+
for (const c of s2) {
145+
if (!cnt[c]) {
143146
return false;
144147
}
148+
cnt[c]--;
145149
}
146150
return true;
147151
}
@@ -150,22 +154,26 @@ function CheckPermutation(s1: string, s2: string): boolean {
150154
#### Rust
151155

152156
```rust
153-
use std::collections::HashMap;
154157
impl Solution {
155158
pub fn check_permutation(s1: String, s2: String) -> bool {
156-
let n = s1.len();
157-
let m = s2.len();
158-
if n != m {
159+
if s1.len() != s2.len() {
159160
return false;
160161
}
161-
let s1 = s1.as_bytes();
162-
let s2 = s2.as_bytes();
163-
let mut map = HashMap::new();
164-
for i in 0..n {
165-
*map.entry(s1[i]).or_insert(0) += 1;
166-
*map.entry(s2[i]).or_insert(0) -= 1;
162+
163+
let mut cnt = vec![0; 26];
164+
for c in s1.chars() {
165+
cnt[(c as usize - 'a' as usize)] += 1;
166+
}
167+
168+
for c in s2.chars() {
169+
let index = c as usize - 'a' as usize;
170+
if cnt[index] == 0 {
171+
return false;
172+
}
173+
cnt[index] -= 1;
167174
}
168-
map.values().all(|i| *i == 0)
175+
176+
true
169177
}
170178
}
171179
```
@@ -179,19 +187,18 @@ impl Solution {
179187
* @return {boolean}
180188
*/
181189
var CheckPermutation = function (s1, s2) {
182-
if (s1.length != s2.length) {
190+
if (s1.length !== s2.length) {
183191
return false;
184192
}
185-
const cnt = new Array(26).fill(0);
186-
for (let i = 0; i < s1.length; ++i) {
187-
const j = s1.codePointAt(i) - 'a'.codePointAt(0);
188-
++cnt[j];
193+
const cnt = {};
194+
for (const c of s1) {
195+
cnt[c] = (cnt[c] || 0) + 1;
189196
}
190-
for (let i = 0; i < s2.length; ++i) {
191-
const j = s2.codePointAt(i) - 'a'.codePointAt(0);
192-
if (--cnt[j] < 0) {
197+
for (const c of s2) {
198+
if (!cnt[c]) {
193199
return false;
194200
}
201+
cnt[c]--;
195202
}
196203
return true;
197204
};
@@ -206,19 +213,18 @@ class Solution {
206213
return false
207214
}
208215

209-
var cnt = Array(repeating: 0, count: 26)
216+
var cnt = [Int](repeating: 0, count: 26)
210217

211218
for char in s1 {
212-
let index = Int(char.asciiValue! - Character("a").asciiValue!)
213-
cnt[index] += 1
219+
cnt[Int(char.asciiValue! - Character("a").asciiValue!)] += 1
214220
}
215221

216222
for char in s2 {
217223
let index = Int(char.asciiValue! - Character("a").asciiValue!)
218-
cnt[index] -= 1
219-
if cnt[index] < 0 {
224+
if cnt[index] == 0 {
220225
return false
221226
}
227+
cnt[index] -= 1
222228
}
223229

224230
return true
@@ -268,8 +274,8 @@ class Solution {
268274
class Solution {
269275
public:
270276
bool CheckPermutation(string s1, string s2) {
271-
sort(s1.begin(), s1.end());
272-
sort(s2.begin(), s2.end());
277+
ranges::sort(s1);
278+
ranges::sort(s2);
273279
return s1 == s2;
274280
}
275281
};
@@ -308,6 +314,31 @@ impl Solution {
308314
}
309315
```
310316

317+
#### JavaScript
318+
319+
```js
320+
/**
321+
* @param {string} s1
322+
* @param {string} s2
323+
* @return {boolean}
324+
*/
325+
var CheckPermutation = function (s1, s2) {
326+
return [...s1].sort().join('') === [...s2].sort().join('');
327+
};
328+
```
329+
330+
#### Swift
331+
332+
```swift
333+
class Solution {
334+
func CheckPermutation(_ s1: String, _ s2: String) -> Bool {
335+
let s1 = s1.sorted()
336+
let s2 = s2.sorted()
337+
return s1 == s2
338+
}
339+
}
340+
```
341+
311342
<!-- tabs:end -->
312343

313344
<!-- solution:end -->

0 commit comments

Comments
 (0)