Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike
1,168 views
in Online Assessments by Expert (44,360 points)

2 Answers

0 like 0 dislike
 
Best answer
1st question:
input string contains 'a' and 'b'. blocks are defined as contiguos elements which have same alphabets. eg, i/p babaa has 4 blocks b, a, b, aa.
number of inserts required to make all blocks of equal length.
by Expert (44,360 points)
0 like 0 dislike
public int solution(String S) {
       char[] myArray = S.toCharArray();
       List<Integer> blockInfo = new ArrayList<>();
       char previous = myArray[0];
       int previousLength  =1;
       int length = myArray.length;
       for(int i=1; i<length; i++){
           if(myArray[i] == previous){
               previousLength++;
           } else {
               blockInfo.add(previousLength);
               previous = myArray[i];
               previousLength = 1;
           }
       }
       blockInfo.add(previousLength);

       int max = blockInfo.get(0);
       for(int i=0; i<blockInfo.size(); i++){
           if(max < blockInfo.get(i)){
               max = blockInfo.get(i);
           }
       }
       int count = 0 ;
       for(int i=0; i< blockInfo.size(); i++){
           count += (max - blockInfo.get(i));
       }

       return count;

   }
by Expert (44,360 points)
...