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
def checkIfOverlap ( str ) : NEW_LINE INDENT length = len ( str ) NEW_LINE visited = [ 0 ] * ( length + 1 ) NEW_LINE for i in range ( length ) : NEW_LINE INDENT if str [ i ] == " . " : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( max ( 0 , i - ord ( str [ i ] ) , min ( length , i + ord ( str [ i ] ) ) + 1 ) ) : NEW_LINE INDENT visited [ j ] += 1 NEW_LINE DEDENT DEDENT for i in range ( length ) : NEW_LINE INDENT if visited [ i ] > 1 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " . 2 . . 2 . " NEW_LINE if checkIfOverlap ( str ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT