Check if a Binary String can be sorted in decreasing order by removing non | Function to sort the given string in decreasing order by removing the non adjacent characters ; Keeps the track whether the string can be sorted or not ; Traverse the given string S ; Check if S [ i ] and S [ i + 1 ] are both '1' ; Traverse the string S from the indices i to 0 ; If S [ j ] and S [ j + 1 ] is equal to 0 ; Mark flag false ; If flag is 0 , then it is not possible to sort the string ; Otherwise ; Driver Code
function canSortString ( S , N ) { let flag = 1 ; let i , j ; for ( let i = N - 2 ; i >= 0 ; i -- ) { if ( S [ i ] == ' ' && S [ i + 1 ] == ' ' ) { break ; } } for ( let j = i ; j >= 0 ; j -- ) { if ( S [ j ] == ' ' && S [ j + 1 ] == ' ' ) { flag = 0 ; break ; } } if ( flag == 0 ) { return " " ; } else { return " " ; } } let S = " " ; let N = S . length ; document . write ( canSortString ( S , N ) ) ;