Google Interview Question
Software EngineersCountry: United States
#include <iostream>
#include <string>
using namespace std;
void print_possible_combination(string input, int pos){
if(pos >= input.size() || input.find('?', pos) == string::npos){
cout << input << endl;
return;
}
else{
pos = input.find('?', pos);
input.replace(pos, 1, "0");
print_possible_combination(input, pos+1);
input.replace(pos, 1, "1");
print_possible_combination(input, pos+1);
}
}
int main(){
string input; cin >> input;
print_possible_combination(input, 0);
}
////Sample Call
Combination("01?01", 0, "");
public static void Combination(string mainSTring, int counter, string combination_string)
{
if (mainSTring.Length == combination_string.Length)
{
System.Console.WriteLine(combination_string);
return;
}
if (mainSTring[counter] == '?')
{
Combination(mainSTring, counter + 1, combination_string + "0");
Combination(mainSTring, counter + 1, combination_string + "1");
return;
}
Combination(mainSTring, counter + 1, combination_string + mainSTring[counter]);
}
def allstr(str1, index, length):
if index == length:
print(str1)
return
if str1[index] == '?':
str1[index] = 'T'
allstr(str1, index + 1, length)
str1[index] = 'X'
allstr(str1, index + 1, length)
str1[index] = '?'
else:
allstr(str1, index + 1, length)
t = list('he?l?o?')
allstr(t, 0, t.__len__())
- rahulroshan96 May 13, 2019