# A Binary String Game A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Given a Binary String S. The task is to determine the winner of the game when two players play a game optimally with the string as per the given conditions:

• Player 1 always starts first.
• Two players take turns choosing a whole block of consecutive equal characters and deleting them from a given binary String S.
• Player 1 can choose only an odd number of consecutive equal characters and Player 2 can choose only an even number of consecutive equal characters. The player may choose nothing and count it as their turn only if it is impossible to choose anything.
• After all the characters are removed, the player with maximum scores wins the game and if scores are equal then print “-1”

Input:_ S = “1110011001010”_

Output:_ Player 1_

Explanation:

The selected characters will be in bold and Player 1’s score is Score_1 and Player 2’s score is Score_2 :

Turn 1 : (Player 1) “1110011001010” → “0011001010” Score_1 = 3

Turn 2 : (Player 2) “0011001010” → “00001010” Score_2 = 2

Turn 3 : (Player 1) “00001010”→ “0000010” Score_1 = 4

_Turn 4: (Player 2) “0000010” _

_He cannot do anything as only one ‘1’ is present which is an odd number. _

Also, he can’t choose the ‘0’s as they are odd (5 and 1), Therefore, Score_2 =2

Turn 5:(Player 1) “0000010”→ “000000” Score_1=5

Turn 6:(Player 2) “000000” → “” Score_2 = 2 (No ‘1’ was deleted in this turn)

Final scores: Score_1 = 5 and Score_2 = 2

Therefore, Player 1 wins.

_Input : _S__=__“11111101”

Output:_ Player 2_

_Explanation: _

Turn 1 : (Player 1) “11111101” → “1111110” Score_1 = 3

Turn 2 : (Player 2) “1111110” → “0” Score_2 = 6

Turn 3 : (Player 1) “0” → “” Score_1 = 3

Final scores: Score_1 = 3 and Score_2 = 6

Therefore, Player 2 wins.

*Approach: *

1. If we observe this game carefully, we understand that the only consecutive 1s are contributing to the scores of these players.
2. Create a list to store the lengths of the consecutive 1s in the string.
3. Sort the list in descending order.
4. Now iterate over the list and if the list element is odd it will be added to the score of the Player 1 and if it is even it will be added to the score of the Player 2.
5. Now if the score of the Player 1 is greater than the score of the Player 2 then print “Player 1” and if the score of the Player 2 is greater than the score of the Player 1 then print “Player 2”.
6. Print “-1” if there is a tie i.e., scores are same.

Below is the implementation of the above approach.

• Java

`// Java program for the above approach`

`**import**` `java.io.*;`

`**import**` `java.util.*;`

`**class**` `GFG {`

`// Function to return the result of`

`// the game`

`**public**` `**static**` `String gameMax(String S)`

`{`

`// length of the string`

`**int**` `N = S.length();`

`// List to maintain the lengths of`

`// consecutive '1's in the string`

`List<Integer> list =` `**new**` `ArrayList<>();`

`// Variable that keeps a track of`

`// the current length of the block`

`// of consecutive '1's`

`**int**` `one =` `0``;`

`**for**` `(``**int**` `i =` `0``; i < N; i++) {`

`**if**` `(S.charAt(i) ==` `'1'``) {`

`one++;`

`}`

`**else**` `{`

`// Adds non-zero lengths`

`**if**` `(one !=` `0``) {`

`list.add(one);`

`}`

`one =` `0``;`

`}`

`}`

`// This takes care of the case`

`// when the last character is '1'`

`**if**` `(one !=` `0``) {`

`list.add(one);`

`}`

`// Sorts the lengths in`

`// descending order`

`Collections.sort(list,`

`Collections.reverseOrder());`

`// Scores of the 2 players`

`**int**` `score_1 =` `0``, score_2 =` `0``;`

`**for**` `(``**int**` `i =` `0``; i < list.size(); i++) {`

`// For player 1`

`**if**` `(list.get(i) %` `2` `==` `1``) {`

`score_1 += list.get(i);`

`}`

`// For player 2`

`**else**` `{`

`score_2 += list.get(i);`

`}`

`}`

`// In case of a tie`

`**if**` `(score_1 == score_2)`

`**return**` `"-1"``;`

`// Print the result`

`**return**` `(score_1 > score_2) ?` `"Player 1"`

`:` `"Player 2"``;`

`}`

`// Driver Code`

`**public**` `**static**` `**void**` `main(String[] args)`

`{`

`// Given string S`

`String S =` `"11111101"``;`

`// Function Call`

`System.out.println(gameMax(S));`

`}`

`}`

Time Complexity:_ O(N)_

_Auxiliary Space: _O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

## Game Development with .NET

.NET is cross-platform. With .NET you can target over 25+ different platforms with a single code base. You can make games for, but not limited to, Windows, macOS, Linux, Android, iOS, Xbox, PlayStation, Nintendo, and mixed reality devices.

## Minimum substring flips required to convert given binary string to another

Minimum Count of Bit flips required to make a Binary String Palindromic ... Given an integer N, the task is to find the minimum number of bits required to be flipped to convert the binary ... _To make “1100” a palindrome, convert the string to “0110”. _ ... Take two pointers to one at the L.S.B *and another to the *M.S.B.

## Count of Binary Strings possible as per given conditions

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Minimum number of operations required to maximize the Binary String

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## Minimum number of flips with rotation to make binary string alternating

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.