Count points which are revisited while following the path specified by a given string
Last Updated :
28 May, 2021
Given a string S representing a sequence of moves(L, R, U, and D) and two integers X and Y representing the starting coordinates, the task is to find the number of positions that are revisited while following the directions specified in the given string according to the following rules:
- If the current character is L, then decrement x-coordinate by 1.
- If the current character is R, then increment x-coordinate by 1.
- If the current character is U, then increment y-coordinate by 1.
- If the current character is D, then decrement y-coordinate by 1.
Examples:
Input: S = “RDDUDL”, X = 0, Y = 0
Output: 2
Explanation: Initially the coordinate is (0, 0). The change of coordinates according to the given order of traversal is as follows:
(0, 0) -> (1, 0) -> (1, -1) -> (1, -2) -> (1, -1) -> (1, -2) -> (0, -2)
Therefore, the number of times a coordinate is revisited is 2.
Input: S = “RDDUDL”, X = 2, Y = 3
Output: 2
Approach: Follow the given steps to solve the problem:
- Initialize a HashSet of pairs that stores the pair of coordinates visited while traversing the given string and the current coordinates in the HashSet.
- Traverse the given string S and perform the following steps:
- Update the value of the coordinates {X, Y} according to the given rule.
- Check if the updated coordinate is present in the HashSet or not. If found to be true, then increment count by 1. Otherwise, continue.
- After completing the above steps, print the value of the count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count(string S, int X, int Y)
{
int N = S.length();
int temp_x = 0, temp_y = 0;
int count = 0;
set<pair< int , int > > s;
s.insert({ X, Y });
for ( int i = 0; i < N; i++) {
temp_x = X;
temp_y = Y;
if (S[i] == 'U' ) {
X++;
}
else if (S[i] == 'D' ) {
X--;
}
else if (S[i] == 'R' ) {
Y++;
}
else {
Y--;
}
if (s.find({ temp_x + X, temp_y + Y })
!= s.end()) {
count++;
}
else {
s.insert({ temp_x + X,
temp_y + Y });
}
}
return count;
}
int main()
{
string S = "RDDUDL" ;
int X = 0, Y = 0;
cout << count(S, X, Y);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int count(String S, int X, int Y)
{
int N = S.length();
int temp_x = 0 , temp_y = 0 ;
int count = 0 ;
HashSet<String> s = new HashSet<>();
s.add((X + "#" + Y));
for ( int i = 0 ; i < N; i++) {
temp_x = X;
temp_y = Y;
if (S.charAt(i) == 'U' ) {
X++;
}
else if (S.charAt(i) == 'D' ) {
X--;
}
else if (S.charAt(i) == 'R' ) {
Y++;
}
else {
Y--;
}
if (s.contains((temp_x + X) + "#"
+ (temp_y + Y))) {
count++;
}
else {
s.add((temp_x + X) + "#" + (temp_y + Y));
}
}
return count;
}
public static void main(String[] args)
{
String S = "RDDUDL" ;
int X = 0 , Y = 0 ;
System.out.print(count(S, X, Y));
}
}
|
Python3
def count(S, X, Y):
N = len (S)
temp_x, temp_y = 0 , 0
count = 0
s = {}
s[(X, Y)] = 1
for i in range (N):
temp_x = X
temp_y = Y
if (S[i] = = 'U' ):
X + = 1
elif (S[i] = = 'D' ):
X - = 1
elif (S[i] = = 'R' ):
Y + = 1
else :
Y - = 1
if ((temp_x + X, temp_y + Y ) in s):
count + = 1
else :
s[(temp_x + X,temp_y + Y )] = 1
return count
if __name__ = = '__main__' :
S = "RDDUDL"
X,Y = 0 , 0
print (count(S, X, Y))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int count(String S, int X, int Y)
{
int N = S.Length;
int temp_x = 0, temp_y = 0;
int count = 0;
HashSet<String> s = new HashSet<String>();
s.Add((X + "#" + Y));
for ( int i = 0; i < N; i++) {
temp_x = X;
temp_y = Y;
if (S[i] == 'U' ) {
X++;
}
else if (S[i] == 'D' ) {
X--;
}
else if (S[i] == 'R' ) {
Y++;
}
else {
Y--;
}
if (s.Contains((temp_x + X) + "#"
+ (temp_y + Y))) {
count++;
}
else {
s.Add((temp_x + X) + "#" + (temp_y + Y));
}
}
return count;
}
public static void Main(String[] args)
{
String S = "RDDUDL" ;
int X = 0, Y = 0;
Console.Write(count(S, X, Y));
}
}
|
Javascript
<script>
function count(S, X, Y){
let N = S.length;
let temp_x = 0, temp_y = 0;
let count = 0;
let s = new Set();
s.add((X + "#" + Y));
for (let i = 0; i < N; i++) {
temp_x = X;
temp_y = Y;
if (S[i] == 'U' ) {
X++;
}
else if (S[i] == 'D' ) {
X--;
}
else if (S[i] == 'R' ) {
Y++;
}
else {
Y--;
}
if (s.has((temp_x + X) + "#"
+ (temp_y + Y))) {
count++;
}
else {
s.add((temp_x + X) + "#" + (temp_y + Y));
}
}
return count;
}
let S = "RDDUDL" ;
let X = 0, Y = 0;
document.write(count(S, X, Y));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...