Maximize absolute displacement from origin by moving on X-axis based on given commands
Given a string S of length N, where each character of the string is either equal to ‘L’, ‘R’ or ‘?’, the task is to find the maximum absolute displacement from the origin by moving following the given commands on X-axis starting from the origin (0, 0):
- ‘L’: Move one unit in the negative X direction.
- ‘R’: Move one unit in the positive X direction.
- ‘?’: Can either move one unit in the negative X or the positive X direction.
Examples:
Input: S = “LL??R”
Output: 3
Explanation:
One of the possible way to move is:
- S[0] = ‘L’, move one unit in -ve X direction, so displacement becomes equal to -1.
- S[1] = ‘L’, move one unit in -ve X direction, so displacement becomes equal to -2.
- S[2] = ‘?’, move one unit in -ve X direction, so displacement becomes equal to -3.
- S[3] = ‘?’, move one unit in -ve X direction, so displacement becomes equal to -4.
- S[4] = ‘R’, move one unit in +ve X direction, so displacement becomes equal to -3.
Therefore, the absolute displacement is abs(-3)=3, and also it is the maximum absolute displacement possible.
Input: S = “?RRR?”
Output: 5
Naive Approach: The simplest approach to solve the problem is to try replacing ‘?’ with either ‘L’ or ‘R’ using recursion and then print the maximum absolute displacement obtained.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int DistRecursion(string S, int i, int dist)
{
if (i == S.length())
return abs (dist);
if (S[i] == 'L' )
return DistRecursion(S, i + 1, dist - 1);
if (S[i] == 'R' )
return DistRecursion(S, i + 1, dist + 1);
return max(DistRecursion(S, i + 1, dist - 1),
DistRecursion(S, i + 1, dist + 1));
}
int maxDistance(string S)
{
return DistRecursion(S, 0, 0);
}
int main()
{
string S = "?RRR?" ;
cout << maxDistance(S);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int DistRecursion(String S, int i, int dist)
{
char [] ch = S.toCharArray();
if (i == ch.length)
return Math.abs(dist);
if (ch[i] == 'L' )
return DistRecursion(S, i + 1 , dist - 1 );
if (ch[i] == 'R' )
return DistRecursion(S, i + 1 , dist + 1 );
return Math.max(DistRecursion(S, i + 1 ,
dist - 1 ),
DistRecursion(S, i + 1 , dist + 1 ));
}
static int maxDistance(String S)
{
return DistRecursion(S, 0 , 0 );
}
public static void main(String[] args)
{
String S = "?RRR?" ;
System.out.print(maxDistance(S));
}
}
|
Python3
def DistRecursion(S, i, dist):
if i = = len (S):
return abs (dist)
if S[i] = = 'L' :
return DistRecursion(S, i + 1 , dist - 1 )
if S[i] = = 'R' :
return DistRecursion(S, i + 1 , dist + 1 )
return max (DistRecursion(S, i + 1 , dist - 1 ),
DistRecursion(S, i + 1 , dist + 1 ))
def maxDistance(S):
return DistRecursion(S, 0 , 0 )
S = "?RRR?"
print (maxDistance(S))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int DistRecursion( string S, int i, int dist)
{
if (i == S.Length)
return Math.Abs(dist);
if (S[i] == 'L' )
return DistRecursion(S, i + 1, dist - 1);
if (S[i] == 'R' )
return DistRecursion(S, i + 1, dist + 1);
return Math.Max(DistRecursion(S, i + 1,
dist - 1),
DistRecursion(S, i + 1, dist + 1));
}
static int maxDistance( string S)
{
return DistRecursion(S, 0, 0);
}
public static void Main()
{
string S = "?RRR?" ;
Console.Write(maxDistance(S));
}
}
|
Javascript
<script>
function DistRecursion(S, i, dist) {
if (i == S.length)
return Math.abs(dist);
if (S[i] == 'L' )
return DistRecursion(S, i + 1, dist - 1);
if (S[i] == 'R' )
return DistRecursion(S, i + 1, dist + 1);
return Math.max(DistRecursion(S, i + 1, dist - 1),
DistRecursion(S, i + 1, dist + 1));
}
function maxDistance(S) {
return DistRecursion(S, 0, 0);
}
let S = "?RRR?" ;
document.write(maxDistance(S));
</script>
|
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the observation that the maximum absolute displacement will be obtained when ‘?’ is replaced with maximum occurring character. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count(string s, char c) {
int ans = 0;
for ( int i = 0; i < s.length(); i++)
{
if (c == s[i])
{
ans++;
}
}
return ans;
}
int maxDistance(string S) {
int l = count(S, 'L' );
int r = count(S, 'R' );
int N = S.length();
return abs (N - min(l, r));
}
int main()
{
string S = "?RRR?" ;
cout << maxDistance(S);
return 0;
}
|
Java
class GFG {
static int maxDistance(String S) {
int l = count(S, 'L' );
int r = count(S, 'R' );
int N = S.length();
return Math.abs(N - Math.min(l, r));
}
private static int count(String s, char c) {
int ans = 0 ;
for ( char i : s.toCharArray())
if (c == i)
ans++;
return ans;
}
public static void main(String[] args) {
String S = "?RRR?" ;
System.out.println(maxDistance(S));
}
}
|
Python3
def maxDistance(S):
l = S.count( 'L' )
r = S.count( 'R' )
N = len (S)
return abs (N - min (l, r))
S = "?RRR?"
print (maxDistance(S))
|
C#
using System;
public class GFG {
static int maxDistance(String S) {
int l = count(S, 'L' );
int r = count(S, 'R' );
int N = S.Length;
return Math.Abs(N - Math.Min(l, r));
}
private static int count(String s, char c) {
int ans = 0;
foreach ( char i in s.ToCharArray())
if (c == i)
ans=ans+1;
return ans;
}
public static void Main(String[] args) {
String S = "?RRR?" ;
Console.WriteLine(maxDistance(S));
}
}
|
Javascript
<script>
function maxDistance(S)
{
var l = count(S, 'L' );
var r = count(S, 'R' );
var N = S.length;
return Math.abs(N - Math.min(l, r));
}
function count(s, c) {
var ans = 0;
for ( var i in s.split( '' ))
if (c == i)
ans++;
return ans;
}
var S = "?RRR?" ;
document.write(maxDistance(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
05 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...