2833. Furthest Point From Origin LeetCode Solution

In this guide, you will get 2833. Furthest Point From Origin LeetCode Solution with the best time and space complexity. The solution to Furthest Point From Origin problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Furthest Point From Origin solution in C++
  4. Furthest Point From Origin solution in Java
  5. Furthest Point From Origin solution in Python
  6. Additional Resources
2833. Furthest Point From Origin LeetCode Solution image

Problem Statement of Furthest Point From Origin

You are given a string moves of length n consisting only of characters ‘L’, ‘R’, and ‘_’. The string represents your movement on a number line starting from the origin 0.
In the ith move, you can choose one of the following directions:

move to the left if moves[i] = ‘L’ or moves[i] = ‘_’
move to the right if moves[i] = ‘R’ or moves[i] = ‘_’

Return the distance from the origin of the furthest point you can get to after n moves.

Example 1:

Input: moves = “L_RL__R”
Output: 3
Explanation: The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves “LLRLLLR”.

Example 2:

Input: moves = “_R__LL_”
Output: 5
Explanation: The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves “LRLLLLL”.

Example 3:

Input: moves = “_______”
Output: 7
Explanation: The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves “RRRRRRR”.

See also  2256. Minimum Average Difference LeetCode Solution

Constraints:

1 <= moves.length == n <= 50
moves consists only of characters 'L', 'R' and '_'.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2833. Furthest Point From Origin LeetCode Solution in C++

class Solution {
 public:
  int furthestDistanceFromOrigin(string moves) {
    int countL = 0;
    int countR = 0;
    int countUnderline = 0;

    for (const char c : moves)
      if (c == 'L')
        ++countL;
      else if (c == 'R')
        ++countR;
      else  // c == '_'
        ++countUnderline;

    return abs(countL - countR) + countUnderline;
  }
};
/* code provided by PROGIEZ */

2833. Furthest Point From Origin LeetCode Solution in Java

class Solution {
  public int furthestDistanceFromOrigin(String moves) {
    int countL = 0;
    int countR = 0;
    int countUnderline = 0;

    for (final char c : moves.toCharArray())
      if (c == 'L')
        ++countL;
      else if (c == 'R')
        ++countR;
      else // c == '_'
        ++countUnderline;

    return Math.abs(countL - countR) + countUnderline;
  }
}
// code provided by PROGIEZ

2833. Furthest Point From Origin LeetCode Solution in Python

class Solution:
  def furthestDistanceFromOrigin(self, moves: str) -> int:
    return abs(moves.count('L') - moves.count('R')) + moves.count('_')
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.