Skip to content

Commit b964819

Browse files
committed
2025-06-22
1 parent 9008b15 commit b964819

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# [2138. 将字符串拆分为若干长度为 k 的组](https://leetcode.cn/problems/divide-a-string-into-groups-of-size-k/description/)
2+
3+
> **日期**:2025-06-22
4+
> **所用时间**:4min
5+
6+
## 1. 模拟
7+
8+
使用循环遍历字符串,将字符串按照长度为 k 的组进行拆分,如果最后一组长度小于 k,则使用 fill 填充。
9+
10+
- 时间复杂度:$O(n)$,其中 $n$ 是输入字符串的长度。需要遍历字符串中的每个字符。
11+
- 空间复杂度:$O(n)$,需要使用列表来存储拆分后的字符串。
12+
13+
**Python3**
14+
15+
```python
16+
class Solution:
17+
def divideString(self, s: str, k: int, fill: str) -> List[str]:
18+
ans = []
19+
for i in range(0, len(s), k):
20+
ans.append(s[i:i+k])
21+
if len(ans[-1]) < k:
22+
ans[-1] += fill * (k - len(ans[-1]))
23+
return ans
24+
```

0 commit comments

Comments
 (0)