2138
This commit is contained in:
parent
b33e3df329
commit
b18a1fcafc
4
.gitignore
vendored
4
.gitignore
vendored
@ -1 +1,3 @@
|
||||
build
|
||||
build
|
||||
compile_commands.json
|
||||
.cache
|
44
2138/2138.cpp
Normal file
44
2138/2138.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user