https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/description/
Divide a String Into Groups of Size k - LeetCode
Divide a String Into Groups of Size k - A string s can be partitioned into groups of size k using the following procedure: * The first group consists of the first k characters of the string, the second group consists of the next k characters of the string,
leetcode.com
1
2
3
4
5
6
7
8
9
10
|
from math import ceil
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
answer = []
for i in range(ceil(len(s)/k)):
if len(s[i*k:(i+1)*k]) == k:
answer.append(s[i*k:(i+1)*k])
else:
answer.append(s[i*k:(i+1)*k] + fill*(k-len(s[i*k:(i+1)*k])))
return answer
|
cs |
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2042. Check if Numbers Are Ascending in a Sentence (0) | 2023.01.13 |
---|---|
leetcode 191. Number of 1 Bits (0) | 2023.01.13 |
leetcode 2180. Count Integers With Even Digit Sum (0) | 2023.01.12 |
leetcode2351. First Letter to Appear Twice (0) | 2023.01.11 |
leetcode 1768. Merge Strings Alternately (0) | 2023.01.11 |
댓글