Check if a cell can be visited more than once in a String | Java 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
import java . io . * ; class GFG { static boolean checkIfOverlap ( String str ) { int len = str . length ( ) ; int [ ] visited = new int [ len + 1 ] ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == ' . ' ) continue ; for ( int j = Math . max ( 0 , i - str . charAt ( i ) ) ; j <= Math . min ( len , i + str . charAt ( i ) ) ; j ++ ) visited [ j ] ++ ; } for ( int i = 0 ; i < len ; i ++ ) { if ( visited [ i ] > 1 ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String str = " . 2 . . 2 . " ; if ( checkIfOverlap ( str ) ) System . out . println ( " YES " ) ; else System . out . print ( " NO " ) ; } }