Count points which are revisited while following the path specified by a given string


In this problem, we have given a string suggesting the moving direction and starting coordinates. We need to find the revisited positions.

We can use the set or map data structure to store the previously visited coordinates. We can say that position is revisited if we find any pair in the set or map.

Problem statement – We have given a string str of length N containing the ‘L’, ‘R’, ‘U’, and ‘D’ characters. Also, we have given X and Y integers representing the starting position. We need to find the total number of coordinates revisited while following the path according to the conditions below.

  • For character ‘L’, move in the left direction by decreasing the value of X by 1.

  • For character ‘R’, move in the right direction by increasing the value of X by 1.

  • For character ‘U’, move in the up direction by increasing the value of Y by 1.

  • For character ‘D’, move downward by decreasing the value of Y by 1.

Sample examples

Input– str = "DDRULRD", X = 0, Y = 0

Output– 2

Explanation– Let’s track the moves according to the given string.

(0, -1) -> (0, -2) -> (1, -2) -> (1, -1) -> (0, -1) ->(1, -1) -> (1, 0).

The above path shows that (0, -1) and (1, -1) are revisited.

Input– str = "RLUDRDDUU", X = 3, Y = 5

Output – 5

Explanation– The path moves are given below.

(4, 5) -> (3, 5) -> (3, 6) -> (3, 5) ->(4, 5) -> (4, 4) -> (4, 3) -> (4, 4) - > (4, 5).

In the above positions, (4, 5) repeated twice, (3,5) repeated twice as the initial position was the same, and (4, 4) repeated once.

Approach 1

In this approach, we will use the set data structure to keep track of moves. We will add +1 or -1 to the X or Y coordinate according to the current character. After updating the position, if we find that position coordinates are already present in the set, we can say it's revisited.

Algorithm

  • Initialize the ‘len’ variable with the length of the string.

  • Initialize the ‘x_pos’ and ‘y_pos’ variables with zero. Define the ‘cnt’ variable to store a number of revisited positions.

  • Define the set, and use the insert() method to insert the pair of initial coordinates

  • Start traversing the string. In the loop, use the if-else statement to update the position according to the current character.

    • If the current character is ‘U’, set y_pos = 1 and x_pos = 0. For character ‘D’, set y_pos = -1, and x_pos = 0;

    • If the current character is ‘R’, set y_pos = 0 and x_pos = 1. For character ‘L’, set y_pos = 0, and x_pos = -1;

  • Add x_pos to X and y_pos to Y.

  • Use the find() method to check whether X and Y exist in the set. If yes, increase the value of ‘cnt’ by 1. Else, insert the pair in the set.

  • Return the ‘cnt’ value.

Example

#include <bits/stdc++.h>
using namespace std;

// function to cnt the total number of revisited positions
int countRevisited(string str, int X, int Y) {
   // Stores the length of the string
   int len = str.length();
   // to store the current position
   int x_pos = 0, y_pos = 0;
   // to store the count of revisited positions
   int cnt = 0;
   // Define a set to store the pairs of visited positions
   set<pair<int, int>> pairs;
   // Insert the starting coordinates
   pairs.insert({X, Y});
   // Traverse over the string
   for (int i = 0; i < len; i++) {
      // Modify the current position according to the given direction.
      if (str[i] == 'U') {
          y_pos = 1;
          x_pos = 0;
      } else if (str[i] == 'D') {
         y_pos = -1;
         x_pos = 0;
      } else if (str[i] == 'R') {
          x_pos = 1;
          y_pos = 0;
      } else {
          x_pos = -1;
          y_pos = 0;
      }
      X += x_pos;
      Y += y_pos;
      // If the current position is already visited, then increment cnt by 1
      if (pairs.find({X , Y }) != pairs.end()) {
          cnt++;
      }
      // else insert the current position in the set
      else {
          pairs.insert({X , Y });
      }
   }
   return cnt;
}
int main(){
   string str = "RLUDRDDUU";
   int X = 3, Y = 5;
   cout << "The numbers of revisited coordinates while following the given path are - " << countRevisited(str, X, Y);
   return 0;
} 

Output

The numbers of revisited coordinates while following the given path are - 5

Time complexity – O(N * logN) as we traverse the string and search for a pair in the set.

Space complexity – O(N) as we need to store N pairs in the worst case.

Approach 2

This approach will use the map data structure to store the visited pairs. Also, we will use the switch case statement in C++ to update the current position according to the character of the string.

Algorithm

  • Define ‘len’, ‘x_pos’, ‘y_pos’, and ‘cnt’ variables.

  • Define the map names ‘pairs’ and add an initial position to the map.

  • Start traversing the string. Use the switch() statement to update the value of the x_pos and y_pos variables according to the character at the ith index.

  • Add the value of x_pos to X and y_pos to Y.

  • Access the value of pair {X, Y} from the map. If it is greater than 0, the position is revisited, and the value of ‘cnt’ is increased by 1. Else set 1 for the current pair in the map.

Example

#include <bits/stdc++.h>
using namespace std;

// function to cnt the total number of revisited positions
int countRevisited(string str, int X, int Y) {
   // Stores the length of the string
   int len = str.length();
   // to store the current position
   int x_pos = 0, y_pos = 0;
   // to store the count of revisited positions
   int cnt = 0;
   // Define a map to store the pairs of visited positions
   map<pair<int, int>, int> pairs;
   // Insert the starting coordinates
   pairs[{X, Y}] = 1;
   // Traverse over the string
   for (int i = 0; i < len; i++) {
      // Modify the current position, according to the given direction using the switch statement
      switch (str[i]){
      case 'U':
          y_pos = 1;
          x_pos = 0;
          break;
      case 'D':
          y_pos = -1;
          x_pos = 0;
          break;
      case 'L':
          y_pos = 0;
          x_pos = -1;
          break;
      case 'R':
          y_pos = 0;
          x_pos = 1;
          break;
      }
      X += x_pos;
      Y += y_pos;
      // If the current position is already visited, then increment cnt by 1
      if (pairs[{X, Y}] > 0) {
          cnt++;
      }
      // else insert the current position in the set
      else {
          pairs[{ X, Y}] = 1;
      }
   }
   return cnt;
}
int main(){
   string str = "RLUDRDDUU";
   int X = 3, Y = 5;
   cout << "The numbers of revisited coordinates while following the given path are - " << countRevisited(str, X, Y);
   return 0;
} 

Output

The numbers of revisited coordinates while following the given path are - 5

Time complexity – O(N*logN), as we iterate the string and search pairs in the map.

Space complexity – O(N), as we use the map.

Updated on: 18-Aug-2023

26 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements