Created
April 1, 2021 17:42
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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