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

by Nitikesh Pattanayak
159 views
A+A-
Reset

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

You may also like

Leave a Reply

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?

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

-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00
Verified by MonsterInsights