Description
- Splits a string into a vector of strings that has consecutive same characters
- Examples in test cases below (
"5500025"
-->["55", "000", "2", "5"]
)
- Examples in test cases below (
- Only ASCII needs to be supported
- In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/
Compile
gcc -std=c++17 file.cpp
- I'm not committed to C++17 (using it for
std::string_view
), so if C++20 or some other version makes it significantly easier, I can switch to that
Test cases
- Provided in
main()
"AAABBCDDDDAAEE5"
-->["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
""
-->[]
Code
file.cpp
(tio.run)
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
std::vector<std::string> grouping;
if (input_string.length() == 0) {
return grouping;
}
std::size_t pos = 0;
while (pos != std::string::npos) {
std::size_t next_pos =
input_string.find_first_not_of(input_string[pos], pos + 1);
std::size_t repeat = next_pos - pos;
if (next_pos == std::string::npos) {
repeat = input_string.length() - pos;
}
grouping.push_back(std::string(repeat, input_string[pos]));
pos = next_pos;
}
return grouping;
}
void run_grouping_test(std::string_view test_case_name,
std::string_view input_string,
const std::vector<std::string> &expected_grouping) {
std::vector<std::string> grouping = group_string_components(input_string);
std::cout << test_case_name << " passed: " << std::boolalpha
<< (grouping == expected_grouping) << "\n";
}
int main() {
run_grouping_test("General case", "AAABBCDDDDAAEE5",
{"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
run_grouping_test("Empty case", "", {});
return 0;
}
```
Description
- Splits a string into a vector of strings that has consecutive same characters
- Examples in test cases below (
"5500025"
-->["55", "000", "2", "5"]
)
- Examples in test cases below (
- Only ASCII needs to be supported
- In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/
Compile
gcc -std=c++17 file.cpp
- I'm not committed to C++17 (using it for
std::string_view
), so if C++20 or some other version makes it significantly easier, I can switch to that
Test cases
- Provided in
main()
"AAABBCDDDDAAEE5"
-->["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
""
-->[]
Code
file.cpp
(tio.run)
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
std::vector<std::string> grouping;
if (input_string.length() == 0) {
return grouping;
}
std::size_t pos = 0;
while (pos != std::string::npos) {
std::size_t next_pos =
input_string.find_first_not_of(input_string[pos], pos + 1);
std::size_t repeat = next_pos - pos;
if (next_pos == std::string::npos) {
repeat = input_string.length() - pos;
}
grouping.push_back(std::string(repeat, input_string[pos]));
pos = next_pos;
}
return grouping;
}
void run_grouping_test(std::string_view test_case_name,
std::string_view input_string,
const std::vector<std::string> &expected_grouping) {
std::vector<std::string> grouping = group_string_components(input_string);
std::cout << test_case_name << " passed: " << std::boolalpha
<< (grouping == expected_grouping) << "\n";
}
int main() {
run_grouping_test("General case", "AAABBCDDDDAAEE5",
{"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
run_grouping_test("Empty case", "", {});
return 0;
}
```
Description
- Splits a string into a vector of strings that has consecutive same characters
- Examples in test cases below (
"5500025"
-->["55", "000", "2", "5"]
)
- Examples in test cases below (
- Only ASCII needs to be supported
- In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/
Compile
gcc -std=c++17 file.cpp
- I'm not committed to C++17 (using it for
std::string_view
), so if C++20 or some other version makes it significantly easier, I can switch to that
Test cases
- Provided in
main()
"AAABBCDDDDAAEE5"
-->["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
""
-->[]
Code
file.cpp
(tio.run)
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
std::vector<std::string> grouping;
if (input_string.length() == 0) {
return grouping;
}
std::size_t pos = 0;
while (pos != std::string::npos) {
std::size_t next_pos =
input_string.find_first_not_of(input_string[pos], pos + 1);
std::size_t repeat = next_pos - pos;
if (next_pos == std::string::npos) {
repeat = input_string.length() - pos;
}
grouping.push_back(std::string(repeat, input_string[pos]));
pos = next_pos;
}
return grouping;
}
void run_grouping_test(std::string_view test_case_name,
std::string_view input_string,
const std::vector<std::string> &expected_grouping) {
std::vector<std::string> grouping = group_string_components(input_string);
std::cout << test_case_name << " passed: " << std::boolalpha
<< (grouping == expected_grouping) << "\n";
}
int main() {
run_grouping_test("General case", "AAABBCDDDDAAEE5",
{"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
run_grouping_test("Empty case", "", {});
return 0;
}
Became Hot Network Question
Description
- Splits a string into a vector of strings that has consecutive same characters
- Examples in test cases below (
"5500025"
-->["55", "000", "2", "5"]
)
- Examples in test cases below (
- Only ASCII needs to be supported
- In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/
Compile
gcc -std=c++17 file.cpp
- I'm not committed to C++17 (using it for
std::string_view
), so if C++20 or some other version makes it significantly easier, I can switch to that
Test cases
- Provided in
main()
"AAABBCDDDDAAEE5"
-->["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
""
-->[]
Code
file.cpp
(tio.run)
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
std::vector<std::string> grouping;
if (input_string.length() == 0) {
return grouping;
}
std::size_t pos = 0;
while (pos != std::string::npos) {
std::size_t next_pos =
input_string.find_first_not_of(input_string[pos], pos + 1);
std::size_t repeat = next_pos - pos;
if (next_pos == std::string::npos) {
repeat = input_string.length() - pos;
}
grouping.push_back(std::string(repeat, input_string[pos]));
pos = next_pos;
}
return grouping;
}
void run_grouping_test(std::string_view test_case_name,
std::string_view input_string,
const std::vector<std::string> &expected_grouping) {
std::vector<std::string> grouping = group_string_components(input_string);
std::cout << test_case_name << " passed: " << std::boolalpha
<< (grouping == expected_grouping) << "\n";
}
int main() {
run_grouping_test("General case", "AAABBCDDDDAAEE5",
{"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
run_grouping_test("Empty case", "", {});
return 0;
}
```
Description
- Splits a string into a vector of strings that has consecutive same characters
- Only ASCII needs to be supported
- In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/
Compile
gcc -std=c++17 file.cpp
- I'm not committed to C++17 (using it for
std::string_view
), so if C++20 or some other version makes it significantly easier, I can switch to that
Test cases
- Provided in
main()
"AAABBCDDDDAAEE5"
-->["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
""
-->[]
Code
file.cpp
(tio.run)
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
std::vector<std::string> grouping;
if (input_string.length() == 0) {
return grouping;
}
std::size_t pos = 0;
while (pos != std::string::npos) {
std::size_t next_pos =
input_string.find_first_not_of(input_string[pos], pos + 1);
std::size_t repeat = next_pos - pos;
if (next_pos == std::string::npos) {
repeat = input_string.length() - pos;
}
grouping.push_back(std::string(repeat, input_string[pos]));
pos = next_pos;
}
return grouping;
}
void run_grouping_test(std::string_view test_case_name,
std::string_view input_string,
const std::vector<std::string> &expected_grouping) {
std::vector<std::string> grouping = group_string_components(input_string);
std::cout << test_case_name << " passed: " << std::boolalpha
<< (grouping == expected_grouping) << "\n";
}
int main() {
run_grouping_test("General case", "AAABBCDDDDAAEE5",
{"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
run_grouping_test("Empty case", "", {});
return 0;
}
```
Description
- Splits a string into a vector of strings that has consecutive same characters
- Examples in test cases below (
"5500025"
-->["55", "000", "2", "5"]
)
- Examples in test cases below (
- Only ASCII needs to be supported
- In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/
Compile
gcc -std=c++17 file.cpp
- I'm not committed to C++17 (using it for
std::string_view
), so if C++20 or some other version makes it significantly easier, I can switch to that
Test cases
- Provided in
main()
"AAABBCDDDDAAEE5"
-->["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
""
-->[]
Code
file.cpp
(tio.run)
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
std::vector<std::string> grouping;
if (input_string.length() == 0) {
return grouping;
}
std::size_t pos = 0;
while (pos != std::string::npos) {
std::size_t next_pos =
input_string.find_first_not_of(input_string[pos], pos + 1);
std::size_t repeat = next_pos - pos;
if (next_pos == std::string::npos) {
repeat = input_string.length() - pos;
}
grouping.push_back(std::string(repeat, input_string[pos]));
pos = next_pos;
}
return grouping;
}
void run_grouping_test(std::string_view test_case_name,
std::string_view input_string,
const std::vector<std::string> &expected_grouping) {
std::vector<std::string> grouping = group_string_components(input_string);
std::cout << test_case_name << " passed: " << std::boolalpha
<< (grouping == expected_grouping) << "\n";
}
int main() {
run_grouping_test("General case", "AAABBCDDDDAAEE5",
{"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
run_grouping_test("Empty case", "", {});
return 0;
}
```
Split string into same consecutive characters
Description
- Splits a string into a vector of strings that has consecutive same characters
- Only ASCII needs to be supported
- In case of style/formatting issues, I am using https://zed0.co.uk/clang-format-configurator/
Compile
gcc -std=c++17 file.cpp
- I'm not committed to C++17 (using it for
std::string_view
), so if C++20 or some other version makes it significantly easier, I can switch to that
Test cases
- Provided in
main()
"AAABBCDDDDAAEE5"
-->["AAA", "BB", "C", "DDDD", "AA", "EE", "5"]
""
-->[]
Code
file.cpp
(tio.run)
#include <cstddef>
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>
group_string_components(std::string_view input_string) {
std::vector<std::string> grouping;
if (input_string.length() == 0) {
return grouping;
}
std::size_t pos = 0;
while (pos != std::string::npos) {
std::size_t next_pos =
input_string.find_first_not_of(input_string[pos], pos + 1);
std::size_t repeat = next_pos - pos;
if (next_pos == std::string::npos) {
repeat = input_string.length() - pos;
}
grouping.push_back(std::string(repeat, input_string[pos]));
pos = next_pos;
}
return grouping;
}
void run_grouping_test(std::string_view test_case_name,
std::string_view input_string,
const std::vector<std::string> &expected_grouping) {
std::vector<std::string> grouping = group_string_components(input_string);
std::cout << test_case_name << " passed: " << std::boolalpha
<< (grouping == expected_grouping) << "\n";
}
int main() {
run_grouping_test("General case", "AAABBCDDDDAAEE5",
{"AAA", "BB", "C", "DDDD", "AA", "EE", "5"});
run_grouping_test("Empty case", "", {});
return 0;
}
```
lang-cpp