Skip to content

Commit 7c52cc0

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.8 MB (63.40%)
1 parent 7aaf349 commit 7c52cc0

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

2260-divide-a-string-into-groups-of-size-k/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<p>A string <code>s</code> can be partitioned into groups of size <code>k</code> using the following procedure:</p>
22

33
<ul>
4-
<li>The first group consists of the first <code>k</code> characters of the string, the second group consists of the next <code>k</code> characters of the string, and so on. Each character can be a part of <strong>exactly one</strong> group.</li>
4+
<li>The first group consists of the first <code>k</code> characters of the string, the second group consists of the next <code>k</code> characters of the string, and so on. Each element can be a part of <strong>exactly one</strong> group.</li>
55
<li>For the last group, if the string <strong>does not</strong> have <code>k</code> characters remaining, a character <code>fill</code> is used to complete the group.</li>
66
</ul>
77

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
# Time: O(max(n, k))
2+
# Space: O(1)
3+
14
class Solution:
25
def divideString(self, s: str, k: int, fill: str) -> List[str]:
3-
additional_chars = len(s) % k
4-
if additional_chars != 0:
5-
additional_chars = k - additional_chars
6-
s += fill * additional_chars
7-
8-
return [s[i: i+k] for i in range(0, len(s), k)]
9-
6+
res = []
7+
n = len(s)
8+
curr = 0
9+
10+
while curr < n:
11+
res.append(s[curr : curr + k])
12+
curr += k
13+
14+
res[-1] += fill * (k - len(res[-1]))
15+
return res
16+

0 commit comments

Comments
 (0)