This is my solution (I needed something to worm up for the hard day:)
#include <iostream>
#include <string>
void print_combinations(const std::string &str)
{
int i, j, k;
int len = str.length();
for (i = 0; i < len - 2; i++)
{
for (j = i + 1; j < len - 1; j++)
{
for (k = j + 1; k < len; k++)
std::cout << str.at(i) << str.at(j) << str.at(k) << std::endl;
}
}
}
void print_rec(const std::string &str, int currLevel, int totalLevel, int startPos, std::string tempString)
{
if (currLevel == totalLevel)
{
std::cout << tempString << std::endl;
return;
}
for (unsigned int i = startPos; i < str.length() - totalLevel + currLevel + 1; i++)
{
tempString.push_back(str.at(i));
print_rec(str, currLevel + 1, totalLevel, i + 1, tempString);
// tempString.pop_back();
tempString.erase(tempString.length() -1, tempString.length());
}
}
int main()
{
print_combinations("testing");
std::cout << std::endl << "====================" << std::endl << std::endl;
std::string tempString = "";
print_rec("testing", 0, 3, 0, tempString);
}
output:
tes
tet
tei
ten
teg
tst
tsi
tsn
tsg
tti
ttn
ttg
tin
tig
tng
est
esi
esn
esg
eti
etn
etg
ein
eig
eng
sti
stn
stg
sin
sig
sng
tin
tig
tng
ing
====================
tes
tet
tei
ten
teg
tst
tsi
tsn
tsg
tti
ttn
ttg
tin
tig
tng
est
esi
esn
esg
eti
etn
etg
ein
eig
eng
sti
stn
stg
sin
sig
sng
tin
tig
tng
ing