Final Destination Hackerearth Problem coderinme

Final Destination

Bob and Khatu are stuck in matrix. The command center sent them a string which decodes to the their final destination. Since Bob and Khatu are not good at problem solving help them to figure out their final destination. They are initially at (0, 0). String contains L, R, U, D denoting left, right, up and down. In each command they will traverse 1 unit distance in the respective direction. For example if they are at (2, 0) and the command is L they will go to (1, 0).

Input:

Input contains a single string.

Output:

Print the final destination location of Bob and Khatu.

Constraints:

1 ≤ |S| ≤ 105
Explanation
if the input string is “LLRDDR”
Initail Postion : 0, 0
1.) ‘L’ -> cover one unit of distance in left direction. New position (-1,0)
2.) ‘L’ -> new position (-2,0)
3.) ‘R’ -> new position (-1,0)
4.) ‘D’ -> new position (-1,-1)
5.) ‘D’ -> new position (-1,-2)
6.) ‘R’ -> new position (0,-2)

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
int x=0,y=0;
cin>>s;
for(int i=0;i<s.length();i++)
{
if(s[i]=='L')
{
x--;

}
else if(s[i]=='R')
{
x++;

} 
else if(s[i]=='U')
{
y++;
}
else if(s[i]=='D')
{

y--;
}
}
cout<<x<<" "<<y;

return 0;
}

 Final Destination

Competitive coding
Hackerearth problem

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.