Skip to content
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

feat: add solutions to lc problems: No.271,276 #2602

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
8 changes: 0 additions & 8 deletions solution/0200-0299/0266.Palindrome Permutation/README.md
Original file line number Diff line number Diff line change
@@ -131,12 +131,4 @@ var canPermutePalindrome = function (s) {

<!-- tabs:end -->

### 方法二:哈希表

利用哈希表来维护元素。遍历字符串每个字母 $s[i]$,若 $s[i]$ 在哈希表中,则将 $s[i]$ 从哈希表中删除,否则将 $s[i]$ 加入哈希表。

遍历结束,若哈希表中元素个数不超过 $1$,则返回 $true$,否则返回 $false$。

时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $|\Sigma|$ 是字符集的大小,本题中字符集为小写字母,因此 $|\Sigma|=26$。

<!-- end -->
58 changes: 16 additions & 42 deletions solution/0200-0299/0271.Encode and Decode Strings/README.md
Original file line number Diff line number Diff line change
@@ -49,9 +49,11 @@

## 解法

### 方法一:使用非 ASCII 码的分隔符
### 方法一:编码字符串长度

Python 中可以直接 `chr(257)` 作为字符串的分隔符,这样就可以实现字符串的编码和解码。
编码时,将字符串的长度转成固定 $4$ 位的字符串,加上字符串本身,依次拼接到结果字符串。

解码时,先取前四位字符串,得到长度,再通过长度截取后面的字符串。依次截取,最终得到字符串列表。

时间复杂度 $O(n)$。

@@ -61,11 +63,21 @@ Python 中可以直接 `chr(257)` 作为字符串的分隔符,这样就可以
class Codec:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string."""
return chr(257).join(strs)
ans = []
for s in strs:
ans.append('{:4}'.format(len(s)) + s)
return ''.join(ans)

def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings."""
return s.split(chr(257))
ans = []
i, n = 0, len(s)
while i < n:
size = int(s[i : i + 4])
i += 4
ans.append(s[i : i + size])
i += size
return ans


# Your Codec object will be instantiated and called as such:
@@ -173,42 +185,4 @@ func (codec *Codec) Decode(strs string) []string {

<!-- tabs:end -->

### 方法二:编码字符串长度

编码时,将字符串的长度转成固定 $4$ 位的字符串,加上字符串本身,依次拼接到结果字符串。

解码时,先取前四位字符串,得到长度,再通过长度截取后面的字符串。依次截取,最终得到字符串列表。

时间复杂度 $O(n)$。

<!-- tabs:start -->

```python
class Codec:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string."""
ans = []
for s in strs:
ans.append('{:4}'.format(len(s)) + s)
return ''.join(ans)

def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings."""
ans = []
i, n = 0, len(s)
while i < n:
size = int(s[i : i + 4])
i += 4
ans.append(s[i : i + size])
i += size
return ans


# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(strs))
```

<!-- tabs:end -->

<!-- end -->
54 changes: 19 additions & 35 deletions solution/0200-0299/0271.Encode and Decode Strings/README_EN.md
Original file line number Diff line number Diff line change
@@ -81,19 +81,35 @@ String[] strs = decoder.decode(msg);

## Solutions

### Solution 1
### Solution 1: Encode String Length

During encoding, we convert the length of the string into a fixed 4-digit string, add the string itself, and append it to the result string in sequence.

During decoding, we first take the first four digits of the string to get the length, and then cut the following string according to the length. We cut it in sequence until we get the list of strings.

The time complexity is $O(n)$.

<!-- tabs:start -->

```python
class Codec:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string."""
return chr(257).join(strs)
ans = []
for s in strs:
ans.append('{:4}'.format(len(s)) + s)
return ''.join(ans)

def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings."""
return s.split(chr(257))
ans = []
i, n = 0, len(s)
while i < n:
size = int(s[i : i + 4])
i += 4
ans.append(s[i : i + size])
i += size
return ans


# Your Codec object will be instantiated and called as such:
@@ -201,36 +217,4 @@ func (codec *Codec) Decode(strs string) []string {

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```python
class Codec:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string."""
ans = []
for s in strs:
ans.append('{:4}'.format(len(s)) + s)
return ''.join(ans)

def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings."""
ans = []
i, n = 0, len(s)
while i < n:
size = int(s[i : i + 4])
i += 4
ans.append(s[i : i + size])
i += size
return ans


# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(strs))
```

<!-- tabs:end -->

<!-- end -->
14 changes: 12 additions & 2 deletions solution/0200-0299/0271.Encode and Decode Strings/Solution.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
class Codec:
def encode(self, strs: List[str]) -> str:
"""Encodes a list of strings to a single string."""
return chr(257).join(strs)
ans = []
for s in strs:
ans.append("{:4}".format(len(s)) + s)
return "".join(ans)

def decode(self, s: str) -> List[str]:
"""Decodes a single string to a list of strings."""
return s.split(chr(257))
ans = []
i, n = 0, len(s)
while i < n:
size = int(s[i : i + 4])
i += 4
ans.append(s[i : i + size])
i += size
return ans


# Your Codec object will be instantiated and called as such:
23 changes: 0 additions & 23 deletions solution/0200-0299/0271.Encode and Decode Strings/Solution2.py

This file was deleted.

Loading