Techno Blender
Digitally Yours.

Make S into an alternate binary string by replacing any character with 0 or 1 up to K times

0 34


  

import java.util.*;

public class GFG {

  

    

    public static void main(String[] args)

    {

  

        

        String str = "akmzaxazmk";

        int K = 6;

  

        

        AlterString(str, K);

    }

  

    

    

    

    static void AlterString(String str, int K)

    {

  

        

        

        boolean flag = true;

  

        

        

        HashMap<Character, Integer> map = new HashMap<>();

  

        

        for (int i = 0; i < str.length(); i++) {

  

            

            

            

            

            

            if (map.containsKey(str.charAt(i))) {

                int end = map.get(str.charAt(i));

                if ((end - i) % 2 == 0) {

                    continue;

                }

                else {

                    flag = false;

                    break;

                }

            }

  

            

            

            

            

            

            else {

                map.put(str.charAt(i), i);

            }

        }

  

        

        

        

        if (flag && map.size() <= K) {

            System.out.println("YES");

            for (Map.Entry<Character, Integer> set :

                 map.entrySet()) {

                System.out.println(

                    " " + set.getKey() + " : "

                    + ((set.getValue() % 2 == 0) ? 0 : 1));

            }

        }

        else {

            System.out.println("NO");

        }

    }

}


  

import java.util.*;

public class GFG {

  

    

    public static void main(String[] args)

    {

  

        

        String str = "akmzaxazmk";

        int K = 6;

  

        

        AlterString(str, K);

    }

  

    

    

    

    static void AlterString(String str, int K)

    {

  

        

        

        boolean flag = true;

  

        

        

        HashMap<Character, Integer> map = new HashMap<>();

  

        

        for (int i = 0; i < str.length(); i++) {

  

            

            

            

            

            

            if (map.containsKey(str.charAt(i))) {

                int end = map.get(str.charAt(i));

                if ((end - i) % 2 == 0) {

                    continue;

                }

                else {

                    flag = false;

                    break;

                }

            }

  

            

            

            

            

            

            else {

                map.put(str.charAt(i), i);

            }

        }

  

        

        

        

        if (flag && map.size() <= K) {

            System.out.println("YES");

            for (Map.Entry<Character, Integer> set :

                 map.entrySet()) {

                System.out.println(

                    " " + set.getKey() + " : "

                    + ((set.getValue() % 2 == 0) ? 0 : 1));

            }

        }

        else {

            System.out.println("NO");

        }

    }

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment