Skip to content

Instantly share code, notes, and snippets.

@sergiosvieira
Created April 1, 2021 17:42
Show Gist options
  • Save sergiosvieira/46c302c7860ea166e77a51cb47586c88 to your computer and use it in GitHub Desktop.
Save sergiosvieira/46c302c7860ea166e77a51cb47586c88 to your computer and use it in GitHub Desktop.
Find different parts of a string when it is sliced into 3 parts.
#include <iostream>
#include <vector>
using std::cout;
using Inputs = std::vector<std::string>;
/*
Find different parts of a string when it is sliced into 3 parts.
*/
int solution(const std::string& s) {
size_t n = s.size() - 2;
size_t size = n * (n + 1) / 2;
size_t i = 0;
size_t j = n;
size_t total = 0;
for (size_t k = 0; k < size; ++k) {
if (i >= j) {
i = 0;
--j;
}
int a = i + 1;
int b = (n + 1) - j;
int c = (n - i) - (n - j);
std::string sa = s.substr(0, a);
std::string sb = s.substr(a, b);
std::string sc = s.substr(a + b, c);
cout << sa << " " << sb << " " << sc << " = ";
if (sa != sb && sb != sc) ++total;
cout << (sa != sb && sb != sc) << '\n';
++i;
}
return total;
}
int main() {
Inputs in = {
"xzxzx"
};
for (auto const& i: in) {
cout << solution(i) << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment