Find the winner of game of repeatedly removing the first character to empty given string | Java program to implement the above approach ; 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
import java . util . * ; class GFG { static void find_Winner ( String [ ] arr , int N ) { @ SuppressWarnings ( " unchecked " ) Vector < Character > [ ] Q = new Vector [ N ] ; for ( int i = 0 ; i < Q . length ; i ++ ) Q [ i ] = new Vector < Character > ( ) ; int M = arr . length ; for ( int i = 0 ; i < M ; i ++ ) { int len = arr [ i ] . length ( ) ; for ( int j = 0 ; j < len ; j ++ ) { Q [ i ] . add ( arr [ i ] . charAt ( j ) ) ; } } int player = 0 ; while ( Q [ player ] . size ( ) > 0 ) { int nextPlayer = Q [ player ] . get ( 0 ) - '0' - 1 ; Q [ player ] . remove ( 0 ) ; player = nextPlayer ; } System . out . print ( " Player ▁ " + ( player + 1 ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; String [ ] arr = { "323" , "2" , "2" } ; find_Winner ( arr , N ) ; } }