Skip to content

Instantly share code, notes, and snippets.

@jasoncoposky
Created March 19, 2020 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasoncoposky/2d001fd7c3d93629b496f74e6554a3ed to your computer and use it in GitHub Desktop.
Save jasoncoposky/2d001fd7c3d93629b496f74e6554a3ed to your computer and use it in GitHub Desktop.
#include <string>
#include <queue>
#include <vector>
#include <iostream>
int main(int argc, char* argv[]) {
std::string allowed_characters{"01?"};
if(argc <=1 ) {
std::cout << "Provide a string comprised of 0, 1 or ?\n";
return 1;
}
// validate input
std::string input{argv[1]};
for(const auto& c : input) {
if(std::string::npos == allowed_characters.find(c)) {
std::cout << "string includes invalid character: [" << c << "]\n";
return 1;
}
}
std::queue<std::string> queue;
queue.push(input);
std::string::size_type pos{};
while(!queue.empty()) {
auto str0{queue.front()};
queue.pop();
// find the next ?
pos = str0.find_first_of('?', pos);
if(std::string::npos == pos) {
std::cout << str0 << "\n";
continue;
}
auto str1{str0};
str0[pos] = '0';
str1[pos] = '1';
queue.push(str0);
queue.push(str1);
} // while
return 0;
} // main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment