본문 바로가기
python-algorithm

leetcode 2138. Divide a String Into Groups of Size k

by 무적김두칠 2023. 1. 12.

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
반응형

댓글