Check if a cell can be visited more than once in a String | Function to check if any cell can be visited more than once ; Array to mark cells ; Traverse the string ; Increase the visit count of the left and right cells within the array which can be visited ; If any cell can be visited more than once , Return True ; Driver code
< ? php function checkIfOverlap ( $ str ) { $ len = strlen ( $ str ) ; $ visited = array_fill ( 0 , $ len + 1 , NULL ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == ' . ' ) continue ; for ( $ j = max ( 0 , $ i - $ str [ $ i ] ) ; $ j <= min ( $ len , $ i + $ str [ $ i ] ) ; $ j ++ ) $ visited [ $ j ] ++ ; } for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ visited [ $ i ] > 1 ) { return true ; } } return false ; } $ str = " . 2 . . 2 . " ; if ( checkIfOverlap ( $ str ) ) echo " YES " ; else echo " NO " ; ? >