Find the last remaining Character in the Binary String according to the given conditions | Javascript implementation of the above approach ; Converting string to array ; Delete counters for each to count the deletes ; Counters to keep track of characters left from each type ; Queue to simulate the process ; Initializing the queue ; Looping till at least 1 digit is left from both the type ; If there is a floating delete for current character we will delete it and move forward otherwise we will increase delete counter for opposite digit ; If 0 are left then answer is 0 else answer is 1 ; Input string ; Length of String ; Printing answer
function remainingDigit ( S , N ) { let c = S . split ( " " ) ; let del = [ 0 , 0 ] ; let count = [ 0 , 0 ] ; let q = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let x = ( c [ i ] == ' ' ? 1 : 0 ) ; count [ x ] ++ ; q . push ( x ) ; } while ( count [ 0 ] > 0 && count [ 1 ] > 0 ) { let t = q . shift ( ) ; if ( del [ t ] > 0 ) { del [ t ] -- ; count [ t ] -- ; } else { del [ t ^ 1 ] ++ ; q . push ( t ) ; } } if ( count [ 0 ] > 0 ) return " " ; return " " ; } let S = " " ; let N = S . length ; document . write ( remainingDigit ( S , N ) ) ;