Determine the winner of a game of deleting Characters from a String
Given a numeric string str, 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.
- In one turn, one player can remove the contiguous elements from the string and the number of elements removed will add up to his score. Player 1 will remove odd contiguous elements and Player 1 will remove even contiguous elements.
- Any player who is not able to make a move loses the game.
- After all the strings are removed, the player with the maximum score wins the game and if the scores are equal, then print “-1”.
Examples:
Input: str = “7788”
Output: -1
Explanation:
The first move for player 1 is to remove 77 and for player 2 it is 88. The score for both is 2. Hence -1.
Input: str = “8822”
Output: Player 2
Explanation:
There are no odd elements, so player 1 can’t make a move, so player 2 wins.
Approach: Follow the steps below in order to solve the problem:
- Create an array A[] of size 10 to store the frequency of all the digits in the given string.
- Iterate the given string and update the frequency of each digit in the above array.
- Traverse the array A[] and take two variables as sum1 = 0 and sum2 = 0 to store the sum of scores.
- Increment sum1 if the index is odd i.e., Player 1 turn, else increment sum2 if the index is even i.e., Player 2 turn.
- After the above steps, check if sum1 is equal to sum2 then print -1 otherwise print the number of players who win the game on the basis of the score.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void determineWinner(string str)
{
vector< int > A(10);
int sum1 = 0, sum2 = 0;
for ( int i = 0; i < str.length(); i++) {
A[ int (str[i]) - 48]++;
}
for ( int i = 0; i <= 9; i++) {
if (i % 2 != 0) {
sum1 = sum1 + A[i];
}
else {
sum2 = sum2 + A[i];
}
}
if (sum1 == sum2) {
cout << "-1" ;
}
else if (sum1 > sum2) {
cout << "Player 1" ;
}
else {
cout << "Player 2" ;
}
}
int main()
{
string str = "78787" ;
determineWinner(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void determineWinner(String str)
{
int [] A = new int [ 10 ];
int sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
A[( int )str.charAt(i) - 48 ]++;
}
for ( int i = 0 ; i <= 9 ; i++)
{
if (i % 2 != 0 )
{
sum1 = sum1 + A[i];
}
else
{
sum2 = sum2 + A[i];
}
}
if (sum1 == sum2)
{
System.out.print( "-1" );
}
else if (sum1 > sum2)
{
System.out.print( "Player 1" );
}
else
{
System.out.print( "Player 2" );
}
}
public static void main (String[] args)
{
String str = "78787" ;
determineWinner(str);
}
}
|
Python3
def determineWinner( str ):
A = [ 0 for i in range ( 9 )];
sum1 = 0 ; sum2 = 0 ;
for i in range ( 0 , len ( str )):
A[ int ( str [i])] + = 1 ;
for i in range ( 0 , 9 ):
if (i % 2 ! = 0 ):
sum1 = sum1 + A[i];
else :
sum2 = sum2 + A[i];
if (sum1 = = sum2):
print ( "-1" );
elif (sum1 > sum2):
print ( "Player 1" );
else :
print ( "Player 2" );
if __name__ = = '__main__' :
str = "78787" ;
determineWinner( str );
|
C#
using System;
class GFG{
static void determineWinner(String str)
{
int [] A = new int [10];
int sum1 = 0, sum2 = 0;
for ( int i = 0; i < str.Length; i++)
{
A[( int )str[i] - 48]++;
}
for ( int i = 0; i <= 9; i++)
{
if (i % 2 != 0)
{
sum1 = sum1 + A[i];
}
else
{
sum2 = sum2 + A[i];
}
}
if (sum1 == sum2)
{
Console.Write( "-1" );
}
else if (sum1 > sum2)
{
Console.Write( "Player 1" );
}
else
{
Console.Write( "Player 2" );
}
}
public static void Main(String[] args)
{
String str = "78787" ;
determineWinner(str);
}
}
|
Javascript
<script>
function determineWinner(str)
{
let A = Array.from({length: 10}, (_, i) => 0);
let sum1 = 0, sum2 = 0;
for (let i = 0; i < str.length; i++)
{
A[str[i].charCodeAt() - 48]++;
}
for (let i = 0; i <= 9; i++)
{
if (i % 2 != 0)
{
sum1 = sum1 + A[i];
}
else
{
sum2 = sum2 + A[i];
}
}
if (sum1 == sum2)
{
document.write( "-1" );
}
else if (sum1 > sum2)
{
document.write( "Player 1" );
}
else
{
document.write( "Player 2" );
}
}
let str = "78787" ;
determineWinner(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) since it is using constant space
Last Updated :
21 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...