Skip to content

feat: add solutions to lcci problems: 01.01~01.06 #2093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lcci/01.01.Is Unique/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

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

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

<!-- tabs:start -->

Expand Down
8 changes: 8 additions & 0 deletions lcci/01.01.Is Unique/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@

## Solutions

**Solution 1: Bit Manipulation**

Based on the examples, we can assume that the string only contains lowercase letters (which is confirmed by actual verification).

Therefore, we can use each bit of a $32$-bit integer `mask` to represent whether each character in the string has appeared.

The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down
6 changes: 3 additions & 3 deletions lcci/01.02.Check Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

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

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

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

Expand All @@ -46,9 +46,9 @@

**方法二:排序**

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

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

<!-- tabs:start -->

Expand Down
20 changes: 20 additions & 0 deletions lcci/01.02.Check Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@

## Solutions

**Solution 1: Array or Hash Table**

First, we check whether the lengths of the two strings are equal. If they are not equal, we directly return `false`.

Then, we use an array or hash table to count the occurrence of each character in string $s1$.

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`.

Finally, after traversing string $s2$, we return `true`.

Note: In this problem, all test case strings only contain lowercase letters, so we can directly create an array of length $26$ for counting.

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$.

**Solution 2: Sorting**

We can also sort the two strings in lexicographical order, and then compare whether the two strings are equal.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

<!-- tabs:start -->

### **Python3**
Expand Down
4 changes: 2 additions & 2 deletions lcci/01.03.String to URL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

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

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

**方法二:模拟**

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

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

<!-- tabs:start -->

Expand Down
12 changes: 12 additions & 0 deletions lcci/01.03.String to URL/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ The missing numbers are [5,6,8,...], hence the third missing number is 8.

## Solutions

**Solution 1: Using `replace()` function**

Directly use `replace` to replace all ` ` with `%20`:

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

**Solution 2: Simulation**

Traverse each character $c$ in the string. When encountering a space, add `%20` to the result, otherwise add $c$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

<!-- tabs:start -->

### **Python3**
Expand Down
2 changes: 1 addition & 1 deletion lcci/01.04.Palindrome Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

**方法一:哈希表**

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

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

Expand Down
6 changes: 6 additions & 0 deletions lcci/01.04.Palindrome Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

## Solutions

**Solution 1: Hash Table**

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

<!-- tabs:start -->

### **Python3**
Expand Down
Loading