Happy Ladybugs – HackerRank Solution

In this post, we will solve Happy Ladybugs HackerRank Solution. This problem (Happy Ladybugs) is a part of HackerRank Problem Solving series.

Task

Happy Ladybugs is a board game having the following properties:

  • The board is represented by a string, b, of length n. The ith character of the string, b[i], denotes the ith cell of the board.
    • If b[i] is an underscore (i.e., _), it means the  cell of the board is empty.
    • If b[i] is an uppercase English alphabetic letter (ascii[A-Z]), it means the ith cell contains a ladybug of color b[i].
    • String b will not contain any other characters.
  • A ladybug is happy only when its left or right adjacent cell (i.e., b[i+1]) 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 n and b for g games of Happy Ladybugs, determine if it’s possible to make all the ladybugs happy. For each game, return YES if all the ladybugs can be made happy through some number of moves. Otherwise, return NO.

Example

b = [Y Y R_B_BR]

You can move the rightmost B and R to make b = [YYRRBB . . . ] and all the ladybugs are happy. Return YES.

Function Description

Complete the happyLadybugs function in the editor below.

happyLadybugs has the following parameters:

  • string b: the initial positions and colors of the ladybugs

Returns

  • string: either YES or NO

Input Format

The first line contains an integer g, the number of games.

The next g pairs of lines are in the following format:

  • The first line contains an integer n, the number of cells on the board.
  • The second line contains a string b that describes the n cells of the board.

Constraints

  • 1 <= g, n <= 100
  • b[i] ∈ {ascii[AZ]}

Sample Input 0

4
7
RBY_YBR
6
X_Y__X
2
__
6
B_RRBR

Sample Output 0

YES
NO
YES
YES

Explanation 0

The four games of Happy Ladybugs are explained below:

  1. Initial board: Now all the ladybugs are happy, so we print YES on a new line.
  2. There is no way to make the ladybug having color Y happy, so we print NO on a new line.
  3. There are no unhappy ladybugs, so we print YES on a new line.
  4. Move the rightmost B and R to form b = [BBRRR . . . ].

Sample Input 1

5
5
AABBC
7
AABBC_C
1
_
10
DD__FQ_QQF
6
AABCBC

Sample Output 1

NO
YES
YES
YES
NO

Solution – Happy Ladybugs – HackerRank Solution

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n , g;
    cin >> g ;
    while(g--){
        int tab[27] = {};
        string s , output = "YES";
        bool tmp = true;
        cin >> n >> s;
        for(int i=0,j=0 ; i<n ; i++, j+=2){
            if(tmp && j<n-1 && s[j]!=s[j+1] && s[j]!=s[j-1]) tmp = false;
            if(s[i]=='_') tab[26]++;
            else tab[s[i]-'A']++;
        }
        for(int i=0 ; i<26 ; i++){
            if(tab[i]==1){
                output = "NO";
                break;
            }
        }
        if(!tmp && tab[26]==0) output = "NO";
        cout << output << endl;
    }
    return 0;
}

Python

import os
import sys
from collections import Counter

#
# Complete the happyLadybugs function below.
#

def is_happy(b):
    if b[0] != b[1] or b[-1] != b[-2]:
        return False
    for ind in range(1, len(b)-1):
        if b[ind] != b[ind-1] and b[ind] != b[ind+1]:
            return False
        
    return True

def happyLadybugs(b):
    cnt = Counter(b)
    print("cnt = {}".format(cnt))
    
    singles = list(filter(lambda x: x[0] != '_' and x[1] == 1, cnt.items()))
    empty = b.count('_')
    
    print("singles = {}".format(singles))
    print("empty = {}".format(empty))
    
    if len(singles) == 0 and empty > 0:
        return 'YES'
    elif len(b) > 2 and is_happy(b):
        return 'YES'
    else:
        return 'NO'
    
    

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    g = int(input())

    for g_itr in range(g):
        n = int(input())

        b = input()

        result = happyLadybugs(b)

        fptr.write(result + '\n')

    fptr.close()

Java

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

public class Solution {

    private static boolean isHappyAlready(char c[]){
        int l=0,r=0;
        for(int i=0;i<c.length;i++){
            l=i-1;
            r=i+1;
            if(l>=0){
                if(c[l]==c[i]) continue;
            }
            if(r<c.length){
                if(c[r]==c[i]) continue;
            }
            return false;
        }
        return true;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int Q = in.nextInt();
        String result="NO";
        for(int a0 = 0; a0 < Q; a0++){
            int n = in.nextInt();
            char[] b = in.next().toCharArray();
            
            char[] cs=new char[27];
            char c=0;
            for(int i=0;i<b.length;i++){
                c=b[i];
                if(c != '_')cs[c-'A']++;
                else cs[26]++;
            }
            result="YES";
            for(int i=0;i<26;i++){
                if(cs[i]==1) {result="NO";break;}
            }
            if(result=="YES"){
               
                if(cs[26]==0 && !isHappyAlready(b)){
                    result="NO";
                }
            }
            System.out.println(result);
        }
        //System.out.println(isHappyAlready("RRR".toCharArray()));
    }
}

Note: This problem (Happy Ladybugs) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *