2833. Furthest Point From Origin

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int furthestDistanceFromOrigin(string moves) {
int lcount = 0, rcount =0, count = 0 ;
for(char c:moves)
{
if(c=='L' || c=='_') lcount++;
if(c=='R' || c=='_') rcount++;
if(c=='_') count++;
}

if(lcount > rcount) rcount-=count;
else lcount-=count;
return abs(lcount - rcount);
}
};

analysis

  • time complexity O(n)
  • space complexity O(1)