Skip to content

Commit b44a398

Browse files
authored
feat: add solutions to lcci problems: 01.01~01.06 (doocs#2093)
1 parent 9180408 commit b44a398

File tree

26 files changed

+663
-550
lines changed

26 files changed

+663
-550
lines changed

lcci/01.01.Is Unique/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
因此,我们可以使用一个 $32$ 位整数 `mask` 的每一位来表示字符串中的每一个字符是否出现过。
3737

38-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度
38+
时间复杂度 $O(n)$,其中 $n$ 为字符串长度。空间复杂度 $O(1)$。
3939

4040
<!-- tabs:start -->
4141

lcci/01.01.Is Unique/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434

3535
## Solutions
3636

37+
**Solution 1: Bit Manipulation**
38+
39+
Based on the examples, we can assume that the string only contains lowercase letters (which is confirmed by actual verification).
40+
41+
Therefore, we can use each bit of a $32$-bit integer `mask` to represent whether each character in the string has appeared.
42+
43+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
44+
3745
<!-- tabs:start -->
3846

3947
### **Python3**

lcci/01.02.Check Permutation/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
**方法一:数组或哈希表**
3434

35-
先判断两个字符串的长度是否相等,若不相等则直接返回 `false`
35+
我们先判断两个字符串的长度是否相等,若不相等则直接返回 `false`
3636

3737
然后用一个数组或哈希表统计字符串 $s1$ 中字符出现的次数。
3838

@@ -46,9 +46,9 @@
4646

4747
**方法二:排序**
4848

49-
按照字典序对两个字符串进行排序,然后比较两个字符串是否相等。
49+
我们也按照字典序对两个字符串进行排序,然后比较两个字符串是否相等。
5050

51-
时间复杂度 $O(n\log n)$,空间复杂度 $O(1)$
51+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度
5252

5353
<!-- tabs:start -->
5454

lcci/01.02.Check Permutation/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@
3434

3535
## Solutions
3636

37+
**Solution 1: Array or Hash Table**
38+
39+
First, we check whether the lengths of the two strings are equal. If they are not equal, we directly return `false`.
40+
41+
Then, we use an array or hash table to count the occurrence of each character in string $s1$.
42+
43+
Next, we traverse the other string $s2$. For each character we encounter, we decrement its corresponding count. If the count after decrementing is less than $0$, it means that the occurrence of characters in the two strings is different, so we directly return `false`.
44+
45+
Finally, after traversing string $s2$, we return `true`.
46+
47+
Note: In this problem, all test case strings only contain lowercase letters, so we can directly create an array of length $26$ for counting.
48+
49+
The time complexity is $O(n)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string, and $C$ is the size of the character set. In this problem, $C=26$.
50+
51+
**Solution 2: Sorting**
52+
53+
We can also sort the two strings in lexicographical order, and then compare whether the two strings are equal.
54+
55+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.
56+
3757
<!-- tabs:start -->
3858

3959
### **Python3**

lcci/01.03.String to URL/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333

3434
直接利用 `replace` 将所有 ` ` 替换为 `%20`
3535

36-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
36+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。
3737

3838
**方法二:模拟**
3939

4040
遍历字符串每个字符 $c$,遇到空格则将 `%20` 添加到结果中,否则添加 $c$。
4141

42-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
42+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。
4343

4444
<!-- tabs:start -->
4545

lcci/01.03.String to URL/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ The missing numbers are [5,6,8,...], hence the third missing number is 8.
4040

4141
## Solutions
4242

43+
**Solution 1: Using `replace()` function**
44+
45+
Directly use `replace` to replace all ` ` with `%20`:
46+
47+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.
48+
49+
**Solution 2: Simulation**
50+
51+
Traverse each character $c$ in the string. When encountering a space, add `%20` to the result, otherwise add $c$.
52+
53+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.
54+
4355
<!-- tabs:start -->
4456

4557
### **Python3**

lcci/01.04.Palindrome Permutation/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
**方法一:哈希表**
2929

30-
我们用哈希表 `cnt` 存储每个字符出现的次数。若次数为奇数的字符超过 $1$ 个,则不是回文排列。
30+
我们用哈希表 $cnt$ 存储每个字符出现的次数。若次数为奇数的字符超过 $1$ 个,则不是回文排列。
3131

3232
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。
3333

lcci/01.04.Palindrome Permutation/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020

2121
## Solutions
2222

23+
**Solution 1: Hash Table**
24+
25+
We use a hash table $cnt$ to store the occurrence count of each character. If more than $1$ character has an odd count, then it is not a palindrome permutation.
26+
27+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.
28+
2329
<!-- tabs:start -->
2430

2531
### **Python3**

0 commit comments

Comments
 (0)