Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2794c8e

Browse files
author
hasibulislam999
committed
Remove Colored Pieces if Both Neighbors are the Same Color problem solved
1 parent cd49153 commit 2794c8e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Title: Remove Colored Pieces if Both Neighbors are the Same Color
3+
* Description: There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'. You are given a string colors of length n where colors[i] is the color of the ith piece.
4+
* Author: Hasibul Islam
5+
* Date: 06/05/2023
6+
*/
7+
8+
/**
9+
* @param {string} colors
10+
* @return {boolean}
11+
*/
12+
function winnerOfGame(colors) {
13+
let aCnt = 0; // length of the current A sequence
14+
let bCnt = 0; // length of the current B sequence
15+
16+
let validA = 0; // total valid Alice moves
17+
let validB = 0; // total valid Bob moves
18+
19+
for (let i = 0; i < colors.length; i++) {
20+
if (colors[i] == "A") {
21+
if (aCnt > 1) validA++;
22+
23+
aCnt++;
24+
bCnt = 0;
25+
} else {
26+
if (bCnt > 1) validB++;
27+
28+
aCnt = 0;
29+
bCnt++;
30+
}
31+
}
32+
33+
return validA > validB;
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /