<script>
function modifyString(S, K)
{
let N = S.length;
for(let i = 0; i < K; i++)
{
let M = new Map();
for(let j = i; j < N; j += K)
{
if (M.has(S[j]))
{
M.set(M.get(S[j]),
M.get(S[j]) + 1);
}
else
{
M.set(S[j], 1);
}
}
if (M.size > 2)
{
return "-1";
}
else if (M.size == 1)
{
if (M.has('?'))
{
for(let j = i; j < N; j += K)
{
S = S.replace(S[j], 'a');
}
}
}
else if (M.size == 2)
{
let ch;
for(let it of M.keys())
{
if (it != '?')
{
ch = it;
}
}
for(let j = i; j < N; j += K)
{
S = S.replace(S[j], ch);
}
}
M.clear();
}
return S;
}
let S = "ab??";
let K = 2;
document.write(modifyString(S, K));
</script>
