This commit is contained in:
Ilya Volchenkov 2025-06-23 00:08:14 +03:00
parent b33e3df329
commit b18a1fcafc
3 changed files with 48 additions and 2 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
build
build
compile_commands.json
.cache

44
2138/2138.cpp Normal file
View 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);
}

View File

@ -1,5 +1,5 @@
build:
cmake -B ./build -S .
cmake -B ./build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=1
cmake --build ./build --config Debug --target all -j 20 --
test: build