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.
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:
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Count the total number of squares that can be visited by Bishop in one move
- Check if any square (with one colored cell) can be divided into two equal parts
- Check if a Queen can attack a given cell on chessboard
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Printing pre and post visited times in DFS of a graph
- Count of elements in first Array greater than second Array with each element considered only once
- Find if string is K-Palindrome or not using all characters exactly once
- Modify the string such that it contains all vowels at least once
- Count of Numbers in Range where the number does not contain more than K non zero digits
- C++ Program for GCD of more than two (or array) numbers
- Prefixes with more a than b
- Element which occurs consecutively in a given subarray more than or equal to K times
- Remove elements from the array which appear more than k times
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- How to store a very large number of more than 100 digits in C++
- Check if a number from every row can be selected such that xor of the numbers is greater than zero
- Maximum number of unique Triplets such that each element is selected only once
- Final cell position in the matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.