Happy Ladybugs is a board game having the following properties:
  • The board is represented by a string, , of length . The  character of the string, , denotes the  cell of the board.
    • If  is an underscore (i.e., _), it means the  cell of the board is empty.
    • If  is an uppercase English alphabetic letter (ascii[A-Z]), it means the  cell contains a ladybug of color .
    • String  will not contain any other characters.
  • A ladybug is happy only when its left or right adjacent cell (i.e., ) is occupied by another ladybug having the same color.
  • In a single move, you can move a ladybug from its current position to any empty cell.
Given the values of  and  for  games of Happy Ladybugs, determine if it's possible to make all the ladybugs happy. For each game, print YES on a new line if all the ladybugs can be made happy through some number of moves. Otherwise, print NO.


As an example, . You can move the rightmost  and  to make  and all the ladybugs are happy.
 Description
Complete the happyLadybugs function in the editor below. It should return an array of strings, either 'YES' or 'NO', one for each test string.
happyLadybugs has the following parameters:
  • b: an array of strings that represents the initial positions and colors of the ladybugs
Input Format
The first line contains an integer , the number of games.
The next  pairs of lines are in the following format:
  • The first line contains an integer , the number of cells on the board.
  • The second line contains a string  describing the  cells of the board.
Output Format
For each game, print YES on a new line if it is possible to make all the ladybugs happy. Otherwise, print NO.


Question from hackerrank.


Solution in C# -



class Solution {

    static string happyLadybugs(string b) {

        if(b.Contains("_") && b.Distinct().Count() == 1)
        return "YES";
        else if(b.Length == 1 && (!b.Contains("_")))
        return "NO";
        else if(!b.Contains("_"))
        {
            bool flag = true;
            for(int i=0; i<b.Length ;i++)
            {
                if(i != b.Length-1 && b[i] == b[i+1])
                    continue;
                else {
                    if(i!=0 && b[i] == b[i-1])
                        continue;
                    else
                    {
                        flag = false;
                        break;
                    }
                }
            }
            if(flag)
                return "YES";
            else
                return "NO";
        }
        else
        {
            char[] arr = b.ToCharArray();
            Dictionary<char,int> dict = new Dictionary<char, int>();
            foreach(char c in arr)
            {
                if(c != '_')
                {
                    if(dict.ContainsKey(c))
                    {
                        int x = dict[c];
                        dict[c] = ++x;
                    }
                    else
                        dict.Add(c,1);
                }
            }

            int min = 0;
            if(dict.Count != 0)
                min = dict.Min(x=>x.Value);
            if(min < 2)
            return "NO";
            else
            return "YES";
        }

    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int g = Convert.ToInt32(Console.ReadLine());

        for (int gItr = 0; gItr < g; gItr++) {
            int n = Convert.ToInt32(Console.ReadLine());

            string b = Console.ReadLine();

            string result = happyLadybugs(b);

            textWriter.WriteLine(result);
        }

        textWriter.Flush();
        textWriter.Close();
    }
}



Post a Comment

Previous Post Next Post