2138: Divide-a-String-Into-Groups-of-Size-k
Easy
table of contents
Just an implementation question.
Simply iterate through and append each k
-sized section
to ans
. If the last section is shorter than k
,
then simply fill up the remaining letters with the fill
letter.
code
class Solution {
public:
<string> divideString(string s, int k, char fill) {
vector<string> ans;
vectorfor (int i = 0; i < s.size(); i += k) {
.push_back(s.substr(i, min((int)s.size()-i, k)));
ans}
int n = ans.back().size();
if (n < k) {
.back().append((k - n), fill);
ans}
return ans;
}
};