Find the winner of game of repeatedly removing the first character to empty given string | Function to find the winner of a game of repeatedly removing the first character to empty a string ; Store characters of each string of the array arr [ ] ; Stores count of strings in arr [ ] ; Traverse the array arr [ ] ; Stores length of current string ; Traverse the string ; Insert arr [ i ] [ j ] ; 1 st Player starts the game ; Stores the player number for the next turn ; Remove 1 st character of current string ; Update player number for the next turn ; Driver Code
function find_Winner ( arr , N ) { var Q = Array . from ( Array ( N ) , ( ) => Array ( ) ) var M = arr . length ; for ( var i = 0 ; i < M ; i ++ ) { var len = arr [ i ] . length ; for ( var j = 0 ; j < len ; j ++ ) { Q [ i ] . push ( arr [ i ] [ j ] - 1 ) ; } } var player = 0 ; while ( Q [ player ] . length > 0 ) { var nextPlayer = Q [ player ] [ 0 ] - ' ' ; Q [ player ] . shift ( ) ; player = nextPlayer ; } document . write ( " " + ( player + 1 ) ) ; } var N = 3 ; var arr = [ " " , " " , " " ] ; find_Winner ( arr , N ) ;