Given a string which consists of either ‘.’ or any number. A ‘.’ in the string means that the cell is empty and if there is a number
in any cell, it means one can move
steps to right or left within the string.
The task is to check if any cell in the string can be visited more than once. If so, print YES otherwise print NO.
Examples:
Input : str = ".2...2.." Output: YES The fourth cell can be visited twice. One way to reach the fourth cell is from 2nd cell by moving 2 steps to right and another way to reach fourth cell is by moving 2 steps left from cell 6. Input : str = ".2...1" Output: NO None of the cells in the given string can be visited more than once.
The idea is to take an array visited[] to keep track of the number of times i-th cell of the string can be visited. Now traverse the string and check if the current character is a ‘.’ or a number
. If the current character is a ‘.’ then do nothing otherwise if it is a number then increase the count of visits in the visited array within the range [i-x, i+x] by 1.
Finally, traverse the visited[] array and check if any cell is visited more than once.
Below is the implementation of the above approach:
C++
// C++ program to check if any cell of the // string can be visited more than once #include <bits/stdc++.h> using namespace std; // Function to check if any cell can be // visited more than once bool checkIfOverlap(string str) { int len = str.length(); // Array to mark cells int visited[len + 1] = { 0 }; // Traverse the string for (int i = 0; i < len; i++) { if (str[i] == '.') continue; // Increase the visit count of the left and right // cells within the array which can be visited 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 any cell can be visited more than once // Return True if (visited[i] > 1) { return true; } } return false; } // Driver code int main() { string str = ".2..2."; if (checkIfOverlap(str)) cout << "YES"; else cout << "NO"; return 0; } |
Java
// Java program to check if any cell of the // string can be visited more than once import java.io.*; class GFG { // Function to check if any cell can be // visited more than once static boolean checkIfOverlap(String str) { int len = str.length(); // Array to mark cells int []visited = new int[len +1]; // Traverse the string for (int i = 0; i < len; i++) { if (str.charAt(i)== '.') continue; // Increase the visit count of the left and right // cells within the array which can be visited 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 any cell can be visited more than once // Return True if (visited[i] > 1) { return true; } } return false; } // Driver code public static void main (String[] args) { String str = ".2..2."; if (checkIfOverlap(str)) System.out.println("YES"); else System.out.print("NO"); } } // This code is contributed by inder_verma.. |
Python 3
# Python3 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 def checkIfOverlap(str) : length = len(str) # Array to mark cells visited = [0] * (length + 1) # Traverse the string for i in range(length) : if str[i] == "." : continue # Increase the visit count # of the left and right cells # within the array which can # be visited for j in range(max(0, i - ord(str[i]), min(length, i + ord(str[i])) + 1)) : visited[j] += 1 # If any cell can be visited # more than once, Return True for i in range(length) : if visited[i] > 1 : return True return False # Driver code if __name__ == "__main__" : str = ".2..2." if checkIfOverlap(str) : print("YES") else : print("NO") # This code is contributed # by ANKITRAI1 |
C#
// C# program to check if any // cell of the string can be // visited more than once using System; class GFG { // Function to check if any cell // can be visited more than once static bool checkIfOverlap(String str) { int len = str.Length; // Array to mark cells int[] visited = new int[len + 1]; // Traverse the string for (int i = 0; i < len; i++) { if (str[i]== '.') continue; // Increase the visit count of // the left and right cells // within the array which can be visited for (int j = Math.Max(0, i - str[i]); j <= Math.Min(len, i + str[i]); j++) visited[j]++; } for (int i = 0; i < len; i++) { // If any cell can be visited // more than once, Return True if (visited[i] > 1) { return true; } } return false; } // Driver code public static void Main () { String str = ".2..2."; if (checkIfOverlap(str)) Console.Write("YES"); else Console.Write("NO"); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
PHP
<?php // PHP 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 function checkIfOverlap($str) { $len = strlen($str); // Array to mark cells $visited = array_fill(0, $len + 1, NULL); // Traverse the string for ($i = 0; $i < $len; $i++) { if ($str[$i] == '.') continue; // Increase the visit count of the // left and right cells within the // array which can be visited for ($j = max(0, $i - $str[$i]); $j <= min($len, $i + $str[$i]); $j++) $visited[$j]++; } for ($i = 0; $i < $len; $i++) { // If any cell can be visited // more than once, Return True if ($visited[$i] > 1) { return true; } } return false; } // Driver code $str = ".2..2."; if (checkIfOverlap($str)) echo "YES"; else echo "NO"; // This code is contributed // by ChitraNayal ?> |
YES
Recommended Posts:
- Check if a Queen can attack a given cell on chessboard
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Check if any square (with one colored cell) can be divided into two equal parts
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Printing pre and post visited times in DFS of a graph
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Count the total number of squares that can be visited by Bishop in one move
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Check if left and right shift of any string results into given string
- Check if a string contains a palindromic sub-string of even length
- Check if a given string is a Reverse Bitonic String or not
- Check if the given string is shuffled substring of another string
- Final cell position in the matrix
- Check if the given string is linear or not
- Check if a given string is Even-Odd Palindrome or not
- Check if a string has m consecutive 1's or 0's
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Check if it is possible to convert one string into another with given constraints
- Check if given Binary string follows then given condition or not
- Check given string is oddly palindrome or not | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


