Minimum digits to remove to make a number Perfect Square | Function to check minimum number of digits should be removed to make this number a perfect square ; Size of the string ; Our final answer ; To store string which is perfect square . ; We make all possible subsequences ; To check jth bit is set or not . ; We do not consider a number with leading zeros ; Convert our temporary string into integer ; Checking temp is perfect square or not . ; Taking maximum sized string ; Print PerfectSquare ; Driver code
function perfectSquare ( s ) { let n = s . length ; let ans = - 1 ; let num = " " ; for ( let i = 1 ; i < ( 1 << n ) ; i ++ ) { let str = " " ; for ( j = 0 ; j < n ; j ++ ) { if ( ( i >> j ) & 1 ) { str = str + s [ j ] ; } } if ( str [ 0 ] != ' ' ) { let temp = 0 ; for ( let j = 0 ; j < str . length ; j ++ ) temp = temp * 10 + ( str [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; k = Math . floor ( Math . sqrt ( temp ) ) ; if ( ( k * k ) == temp ) { if ( ans < str . length ) { ans = str . length ; num = str ; } } } } if ( ans == - 1 ) return ans ; else { document . write ( num + " " ) ; return ( n - ans ) ; } } document . write ( perfectSquare ( " " ) + " " ) ; document . write ( perfectSquare ( " " ) + " " ) ;