Directi Interview Question
SDE1sCountry: India
Interview Type: Written Test
s=list(raw_input())
f=False
for i in range(0,len(s)):
if s[i]=='?':
p=s[i-1]
q=s[i+1]
f=True
if p!='a' and q!='a':
s[i]='a';
elif p!='b' and q!='b':
s[i]='b';
else:
s[i]='c';
if f:
print ''.join(s);
else:
print "Not Possible"
For a character X in the string
1) let p(X) be its predecessor, if X is not the first, or the last character otherwise;
2) let s(X) be its successor, if X is not the last, or the first character otherwise.
For each character X (from first to last), if X = '?' then replace it with the first of 'a','b' or 'c' that is distinct from p(X) and s(X). At each iteration check also whether X is distinct from p(X) and s(X) and, if not, output "Not Possible" and stop.
- cassio.neri August 16, 2014