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
function checkIfOverlap ( str ) { let len = str . length ; let visited = new Array ( len + 1 ) ; for ( let i = 0 ; i < visited . length ; i ++ ) { visited [ i ] = 0 ; } for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) continue ; for ( let j = Math . max ( 0 , i - str [ i ] ) ; j <= Math . min ( len , i + str [ i ] ) ; j ++ ) visited [ j ] ++ ; } for ( let i = 0 ; i < len ; i ++ ) { if ( visited [ i ] > 1 ) { return true ; } } return false ; } let str = " " ; if ( checkIfOverlap ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;