Divide a String Into Groups of Size k LeetCode solution in C++

1
class Solution {
2
public:
3
vector<string> divideString(string s, int k, char fill) {
4
vector<string> v;
5
for (int i = 0; i < s.size(); i = i + k) {
6
string t = s.substr(i, k); // make substring of size atmost k
7

8
if (t.size() == k) // if size if k then push
9
{
10
v.push_back(t);
11
continue;
12
}
13

14
int l = t.size(); // if it is the last group and size if less than k
15
for (int j = 0; j < (k - l); j++) // add fill char to t to make size k
16
t += fill;
17

18
v.push_back(t);
19
}
20
return v;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0