In this HackerEarth Meeting the origin problem solution You are given a string path S which consists of only four characters ‘L’, ‘R’, ‘U’, and ‘D’. You are initially at origin (0,0). You have to follow the directions as provided in path S in order. For ‘L’ you have to move 1 unit left, for ‘R’ move 1 unit right, for ‘U’ move 1 unit up and for ‘D’ move 1 unit down.
You can do these operations any number of times:
Delete any single move and remove it from the string path .
Convert any single move ‘L’ to ‘R’ or vice-versa.
Convert any single move ‘U’ to ‘D’ or vice-versa.
Print the minimum operations required, after which going through the new path, you will meet the origin again.
HackerEarth Meeting the origin problem solution.
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)
typedef double db;
typedef long long ll;
const int N=2e5+5;
const int mod=1e9+7;
void solve(){
string s;
cin>>s;
int l=count(all(s),'L'),r=count(all(s),'R'),u=count(all(s),'U'),d=count(all(s),'D');
int ans=(l+r)%2+(u+d)%2+abs(l-r)/2+abs(u-d)/2;
cout<<ans<<'n';
}
signed main(){
FastIO;
int t;
cin>>t;
while(t--) solve();
return 0;
}
Second solution
t = int(input())
while t > 0:
t -= 1
s = input()
print((abs(s.count('L') - s.count('R')) + 1) // 2 + (abs(s.count('U') - s.count('D')) + 1) // 2)