Reduce price to transform given string into concatenation of equal substrings of size Ok

by admin

  

import java.util.*;

import java.lang.*;

  

class GFG {

  

    

    

    

    static void minCost(String s, int okay)

    {

        

        int n = s.size();

  

        

        int ans = 0;

  

        

        

        for (int i = 0; i < okay; i++) {

  

            

            int[] a = new int[26];

  

            for (int j = i; j < n; j += okay) {

                a[s.charAt(j) - 'a']++;

            }

  

            

            

            int min_cost

                = Integer.MAX_VALUE;

  

            

            for (int ch = 0; ch < 26; ch++) {

  

                int price = 0;

  

                

                

                for (int tr = 0; tr < 26; tr++)

                    price += Math.abs(ch - tr)

                            * a[tr];

  

                

                

                min_cost = Math.min(min_cost,

                                    price);

            }

  

            

            ans += min_cost;

        }

  

        

        

        System.out.println(ans);

    }

  

    

    public static void predominant(String[] args)

    {

        

        String S = "abcdefabc";

  

        int Ok = 3;

  

        

        minCost(S, Ok);

    }

}

Related Posts

Leave a Comment