Lexicographically smallest string with period K possible by replacing ‘?’s from a given string

<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>

Related posts

How to recover your hacked wordpress site

7 Best Automation Testing Tools to Consider in 2021

Minimum number of increment / decrements required to be performed on one of the two given numbers to make them non-coprime

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More