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
< ? php function perfectSquare ( $ s ) { $ n = strlen ( $ s ) ; $ ans = -1 ; $ num = " " ; for ( $ i = 1 ; $ i < ( 1 << $ n ) ; $ i ++ ) { $ str = " " ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( ( $ i >> $ j ) & 1 ) { $ str = $ str . $ s [ $ j ] ; } } if ( $ str [ 0 ] != '0' ) { $ temp = 0 ; for ( $ j = 0 ; $ j < strlen ( $ str ) ; $ j ++ ) $ temp = $ temp * 10 + ( ord ( $ str [ $ j ] ) - ord ( '0' ) ) ; $ k = ( int ) ( sqrt ( $ temp ) ) ; if ( ( $ k * $ k ) == $ temp ) { if ( $ ans < strlen ( $ str ) ) { $ ans = strlen ( $ str ) ; $ num = $ str ; } } } } if ( $ ans == -1 ) return $ ans ; else { echo ( $ num . " " ) ; return ( $ n - $ ans ) ; } } echo ( perfectSquare ( "8314" ) . " STRNEWLINE " ) ; echo ( perfectSquare ( "753" ) . " STRNEWLINE " ) ; ? >