Minimum Boys Next to Girls
Updated
Problem statement
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.
Function
minBoysNextToGirls(s: String) → intExamples
Example 1
s = "-G-GG--"return = 2One optimal placement is -GBGGB- with boys at indices 2 and 5.
Example 2
s = "G-G"return = 1Placing one boy in the middle seat covers both girls.
Example 3
s = "G"return = -1Constraints
1 <= s.length <= 2 * 10^5s[i]is either'G'or'-'.- Adjacency only means immediate left or immediate right.
Similar questions at other companies
- Maximum String OperationsMicrosoft · OAMicrosoft interview questions
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Longest Non-Decreasing Substring After Pair SwapsVisa · OAVisa interview questions
- Minimum Deletion Cost for Distinct Adjacent CharactersHSBC · OAHSBC interview questions
- Minimum Binary Swaps to a PalindromeAT&T · OAAT&T interview questions
- Robot Assembly Line ConflictsGoogle · OAGoogle interview questions
- Substring RemovalJP Morgan Chase · OAJP Morgan Chase interview questions
- Minimum Additions to Make a Valid ABC StringIBM · OAIBM interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Minimum Adjacent TranspositionsEpic · OAEpic interview questions