Final Destination

Bob and Khatu are stuck in the matrix. The command center sent them a string that decodes to 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). The 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 they will go to (1, 0).

Example:

Input:  s = "LLRDDR"
Output: 0 -2

Approach

C++

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

void finalDestination(string s)
{
    int L = 0R = 0U = 0D = 0;
    int n = s.size();
    for (int i = 0i < ni++)
    {
        if (s[i] == 'L')
            L++;
        else if (s[i] == 'R')
            R++;
        else if (s[i] == 'U')
            U++;
        else
            D++;
    }
    cout << R - L << " " << U - D << "\n";
}
int main()
{
    string s = "LLRDDR";

    finalDestination(s);

    return 0;
}


No comments:

Post a Comment