Skip to content

2833. Furthest Point From Origin 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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;
  }
}
1
2
3
class Solution:
  def furthestDistanceFromOrigin(self, moves: str) -> int:
    return abs(moves.count('L') - moves.count('R')) + moves.count('_')