Hackerrank - Lady Bug

 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, return YES if all the ladybugs can be made happy through some number of moves. Otherwise, return NO.
Example

You can move the rightmost  and  to make  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 , 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  that describes the  cells of the board.

Constraints

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:
    lady.png
    After the first move:
    lady(1).png
    After the second move:
    lady(2).png
    After the third move:
    lady(3).png
    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  and  to form .

Sample Input 1

5
5
AABBC
7
AABBC_C
1
_
10
DD__FQ_QQF
6
AABCBC

Sample Output 1

NO
YES
YES
YES
NO
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

     public static String happyLadybugs(String b) {

        System.out.println(b);
        List<Character> bugs = new ArrayList<>();

        for (Character bug : b.toCharArray()) {
            bugs.add(bug);
        }

        for (int index = 0; index < bugs.size(); index++) {
            if (Collections.frequency(bugs, bugs.get(index)) == 1 && bugs.get(index) != '_')
                return "NO";
        }

        if (bugs.contains('_'))
            return "YES";
        else {
            int index = 0;
            int count = 1;
            while (index < bugs.size() - 1) {
                // System.out.println(index);
                // System.out.println(count);
                if (bugs.get(index) == bugs.get(index + 1)) {
                    index++;
                    count++;
                } else {
                    if (count < 2) {
                        return "NO";
                    } else {
                        count = 1;
                        index++;
                    }
                }

            }

        }

        return "YES";
    }





}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int g = Integer.parseInt(bufferedReader.readLine().trim());

        IntStream.range(0, g).forEach(gItr -> {
            try {
                int n = Integer.parseInt(bufferedReader.readLine().trim());

                String b = bufferedReader.readLine();

                String result = Result.happyLadybugs(b);

                bufferedWriter.write(result);
                bufferedWriter.newLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        bufferedReader.close();
        bufferedWriter.close();
    }
}


Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Hackerrank - Day of the Programmer

Hackerrank - UNIQUE ARMSTRONG NUMBER