我正在练习我的编码技能,我正在解决以下回溯问题(原始指南的解决方案也在那里)。
问题总结:
给定一个字符串,您需要打印所有可能的字符串,这些字符串可以通过在它们之间放置空格(零或一)来构成。
我的解决方案如下:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
void permutationWithSpacesAux(const char* s, string buf, int s_index, int b_index, int len_s){
// stop condition
if(s_index == len_s){
cout << buf << endl;
return;
}
// print w\o space
buf[b_index] = s[s_index];
// recursive call w\ next indices
permutationWithSpacesAux(s, buf, s_index + 1, b_index + 1, len_s);
// print w\ space
buf[b_index] = ' ';
buf[b_index + 1] = s[s_index];
// recursive call w\ next …Run Code Online (Sandbox Code Playgroud)