44 lines
1.2 KiB
C++
44 lines
1.2 KiB
C++
#include <algorithm>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
class Solution {
|
|
public:
|
|
std::vector<std::string> divideString(std::string s, int k, char fill) {
|
|
std::vector<std::string> res;
|
|
for (size_t i = 0; i < s.length(); i += k) {
|
|
auto substr = s.substr(i, std::min<size_t>(k, s.length() - i));
|
|
if (substr.length() < k) {
|
|
auto n = substr.length();
|
|
substr.resize(k);
|
|
std::fill_n(substr.begin() + n, k - n, fill);
|
|
}
|
|
res.emplace_back(substr);
|
|
}
|
|
return res;
|
|
}
|
|
};
|
|
}
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
TEST(P2138, 1) {
|
|
std::string input = "abcdefghi";
|
|
std::vector<std::string> expected = { "abc","def","ghi" };
|
|
int k = 3;
|
|
char fill = 'x';
|
|
Solution s;
|
|
auto res = s.divideString(input, k, fill);
|
|
EXPECT_EQ(res, expected);
|
|
}
|
|
|
|
TEST(P2138, 2) {
|
|
std::string input = "abcdefghij";
|
|
std::vector<std::string> expected = { "abc","def","ghi","jxx" };
|
|
int k = 3;
|
|
char fill = 'x';
|
|
Solution s;
|
|
auto res = s.divideString(input, k, fill);
|
|
EXPECT_EQ(res, expected);
|
|
} |