Happy Ladybugs

                      Hacker Rank Solution


 Happy Ladybugs

Hey guys ,welcome to another blog on hacker rank solution. this time we gonna discuss a hacker rank algorithim problem, which is named as Happy Ladybugs.


Problem:

Problem Breakdown:

First of all let us understand the problem statement----
As described in the question that ladybugs is board game and the board is represented by a String b , each Charcter of string shows the type(by color) of ladybug(insect) at that place. for ex if any charcter of the String is 'X' ,then that ladybug is of color X.

There can be any of the 26 types of ladybugs (i.e. UpperCase alphabates i.e. A to Z)
If any character of string is underscore (' _ ') then it represent an empty place.



As described above that we have to determine that all the ladybugs in the string are happy or not ,if not then they can be made happy through some no. of moves ?

Solution:

Have look at this test case:




Some points to be noted.....

  • we can only make moves if there exist a empty place or in other word a  underscore('_').
  • it should be noted down that if there exist any empty place in the String ,then it is also possible to arrange ladybugs of same color together to make them happy.
  • To make a ladybug happy there should exist one or more ladybug with the same color.i.e. there should be atleast 2 ladybug of each  color type to make every ladybug happy.

Have a look at Some of this test Cases:

_       -> YES
X       -> NO
XX      -> YES
X_      -> NO
XY      -> NO
X_X     -> YES
XYX     -> NO
XYZ     -> NO
XXYY    -> YES
XXYZ    -> NO

 Have a look at last test case , it should be noted that , we cant make any changes to this string as there is no empty place in the string, so the only way to find that everylady bug in this string is happy  is by checking that there is a charcter in either left or right with same the  value as itself.

By keeping these points in the mind the java code can be written as

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static boolean ladyBugs(String s,int n)
    {   boolean flag_uscores=false,flag_letter=false;
            int[] arr=new int[26];
            for(int i=0;i<n;i++)
            {
                if(s.charAt(i)=='_')
                    flag_uscores=true;
                else
                    flag_letter=true;
                if(s.charAt(i)!='_')
                    arr[s.charAt(i)-'A']++;
            }
            if(flag_uscores==true&&flag_letter==false)
                return true;
            else if(flag_uscores==false&&flag_letter==true)
            {   if(s.length()==1)
                return false;
                if(s.charAt(0)!=s.charAt(1)||s.charAt(n-2)!=s.charAt(n-1))
                return false;
                else
                for(int i=1;i<n-1;i++)
                {
                    if(!(s.charAt(i)==s.charAt(i-1)||s.charAt(i)==s.charAt(i+1)))
                        return false;
                }
                return true;
            }
            else
            {
                for(int i=0;i<26;i++)
                    if(arr[i]==1)
                        return false;
                return true;
            }
    }
        
            
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int Q = in.nextInt();
        for(int a0 = 0; a0 < Q; a0++){
            int n = in.nextInt();
            String b = in.next();
            boolean result=ladyBugs(b,n);
            if(result==true)
                System.out.println("YES");
            else
                System.out.println("NO");
        }
    }
}

Some points regarding the code

  • The array 'arr' represent the count of each charcter in the string
  • The flag_uscores if there is any under score in the string or not
  • the flag_letter represent if there is any letter in the string.

If still you are not able to understand this solution , then you are in the wrong field mate ,better you try something else.

Link-to-the-hacker-rank-question

Follow me on social media:-
Facebook , Instagram .

For more such type of solutions ,do follow this blog.

Comments

Popular posts from this blog

Strange Counter

The Hurdle Race