Check if a cell can be visited more than once in a String | C ++ program to check if any cell of the string can be visited more than once ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkIfOverlap ( string str ) { int len = str . length ( ) ; int visited [ len + 1 ] = { 0 } ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' . ' ) continue ; for ( int j = max ( 0 , i - str [ i ] ) ; j <= min ( len , i + str [ i ] ) ; j ++ ) visited [ j ] ++ ; } for ( int i = 0 ; i < len ; i ++ ) { if ( visited [ i ] > 1 ) { return true ; } } return false ; } int main ( ) { string str = " . 2 . . 2 . " ; if ( checkIfOverlap ( str ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }