Happy Ladybugs

Happy Ladybugs

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 ith 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., ) 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

b=[YYR_B_BR]

You can move the rightmost B and R to make b=[YYRRBB__BR__] 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

Sample Input 0

1
2
3
4
5
6
7
8
9
4
7
RBY_YBR
6
X_Y__X
2
__
6
B_RRBR

Sample Output 0

1
2
3
4
YES
NO
YES
YES

Explanation 0

The four games of Happy Ladybugs are explained below:

  1. Initial board:

    After the first move:

    After the second move:

    After the third move:

    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

1
2
3
4
5
6
7
8
9
10
11
5
5
AABBC
7
AABBC_C
1
_
10
DD__FQ_QQF
6
AABCBC

Sample Output 1

1
2
3
4
5
NO
YES
YES
YES
NO

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Complete the 'happyLadybugs' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING b as parameter.
*/

function happyLadybugs(b) {
// Write your code here
let result = {};
let isUnderscore = false;

for (let i = 0; i < b.length; i++) {
if (b[i] === '_') {
isUnderscore = true;
continue;
}

if (!result[b[i]]) {
result[b[i]] = 0;
}

result[b[i]]++;
}

if (!isUnderscore) {
for (let i = 1; i < b.length - 1; i++) {
if (b[i - 1] !== b[i] && b[i] !== b[i + 1]) {
return 'NO';
}
}
}

for (const [key, value] of Object.entries(result)) {
if (value === 1) return 'NO';
}

return 'YES';
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×