FastPrepFastPrep
Problem Brief

Minimum Boys Next to Girls

FULLTIMEOA

You are given a row of seats represented by a string s.

  • 'G' means the seat is already occupied by a girl.
  • '-' means the seat is empty, and you may place a boy there.

Place the minimum number of boys so that every girl has at least one adjacent boy immediately to her left or right. Boys can only be placed in empty seats.

Return the minimum number of boys required. If it is impossible to satisfy every girl, return -1.

1Example 1

Input
s = "-G-GG--"
Output
2
Explanation

One optimal placement is -GBGGB- with boys at indices 2 and 5.

2Example 2

Input
s = "G-G"
Output
1
Explanation

Placing one boy in the middle seat covers both girls.

3Example 3

Input
s = "G"
Output
-1

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= s.length <= 2 * 10^5
  • s[i] is either 'G' or '-'.
  • Adjacency only means immediate left or immediate right.
public int minBoysNextToGirls(String s) {
    // write your code here
}
Input

s

"-G-GG--"

Output

2

Sign in to submit your solution.