Circular movement in a Path
Last Updated :
17 Aug, 2023
Given a string S, consisting of only 3 characters ‘A‘, ‘C‘, or ‘?‘. ‘A’ represents anti-clockwise movement, ‘C’ represents clockwise movement, and ‘?’ any movement, the task is to find the maximum distance you can travel in any of the directions from the initial position.
Examples:
Input: S = “AC?C”
Output: 2
Input: S = “?C??CAC??”
Output: 7
Input: S = “ACCA?C???”
Output: 5
Approach: This can be solved with the following idea:
The idea is to keep track of the number of ‘A’s and ‘C’s encountered so far using two variables currA and currC. Whenever an ‘A’ is encountered, currA will be incremented and currC will be decremented, and vice versa when a ‘C’ is encountered. If a ‘?’ is encountered, both currA and currC will be incremented. we will also keep track of the maximum difference between currA and currC encountered so far, using two variables maxA and maxC. In the end, the code returns the maximum of maxA and maxC as the result.
Steps involved in the implementation of code:
- Define a function named maxDistance that takes a string as input and returns an integer. The purpose of this function is to find the maximum distance between the number of ‘A’s and ‘C’s in the given string.
- Inside the function, initialize four integer variables currA, currC, maxA, and maxC to zero. currA and currC will be used to keep track of the number of ‘A’s and ‘C’s encountered so far, respectively, while maxA and maxC will be used to keep track of the maximum difference between ‘A’s and ‘C’s and ‘C’s and ‘A’s encountered so far, respectively.
- Use a for loop to iterate through each character in the input string.
- Inside the for loop, use if-else statements to handle the following cases:
- If the current character is ‘A’, increment currA and decrement currC.
- If the current character is ‘C’, increment currC and decrement currA.
- If the current character is neither ‘A’ nor ‘C’ (i.e., ‘?’), increment both currA and currC.
- After updating currA and currC, use the max function to update maxA and maxC, respectively.
- Finally, outside the for loop, return the maximum of maxA and maxC as the output of the function.
Below is the implementation of the code:
C++
#include <iostream>
#include <string>
using namespace std;
int maximumdistance(string str)
{
int N = str.size();
int currA = 0;
int currC = 0;
int maxA = 0;
int maxC = 0;
for ( int i = 0; i < N; i++) {
if (str[i] == 'A' ) {
currA++;
currC--;
}
else if (str[i] == 'C' ) {
currC++;
currA--;
}
else {
currC++;
currA++;
}
maxA = max(maxA, currA);
maxC = max(maxC, currC);
}
return max(maxA, maxC);
}
int main()
{
string Str = "A?ACAC??A" ;
cout << maximumdistance(Str);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int maximumdistance(String str)
{
int N = str.length();
int currA = 0 ;
int currC = 0 ;
int maxA = 0 ;
int maxC = 0 ;
for ( int i = 0 ; i < N; i++) {
if (str.charAt(i) == 'A' ) {
currA++;
currC--;
}
else if (str.charAt(i) == 'C' ) {
currC++;
currA--;
}
else {
currC++;
currA++;
}
maxA = Math.max(maxA, currA);
maxC = Math.max(maxC, currC);
}
return Math.max(maxA, maxC);
}
public static void main(String[] args)
{
String Str = "A?ACAC??A" ;
System.out.println(maximumdistance(Str));
}
}
|
Python3
def maximumdistance(s: str ) - > int :
N = len (s)
currA = 0
currC = 0
maxA = 0
maxC = 0
for i in range (N):
if s[i] = = 'A' :
currA + = 1
currC - = 1
elif s[i] = = 'C' :
currC + = 1
currA - = 1
else :
currC + = 1
currA + = 1
maxA = max (maxA, currA)
maxC = max (maxC, currC)
return max (maxA, maxC)
if __name__ = = '__main__' :
Str = "A?ACAC??A"
print (maximumdistance( Str ))
|
C#
using System;
class GFG {
static int maximumdistance( string str)
{
int N = str.Length;
int currA = 0;
int currC = 0;
int maxA = 0;
int maxC = 0;
for ( int i = 0; i < N; i++) {
if (str[i] == 'A' ) {
currA++;
currC--;
}
else if (str[i] == 'C' ) {
currC++;
currA--;
}
else {
currC++;
currA++;
}
maxA = Math.Max(maxA, currA);
maxC = Math.Max(maxC, currC);
}
return Math.Max(maxA, maxC);
}
public static void Main()
{
string Str = "A?ACAC??A" ;
Console.WriteLine(maximumdistance(Str));
}
}
|
Javascript
function maximumdistance(str) {
let N = str.length;
let currA = 0;
let currC = 0;
let maxA = 0;
let maxC = 0;
for (let i = 0; i < N; i++) {
if (str[i] === 'A' ) {
currA++;
currC--;
}
else if (str[i] === 'C' ) {
currC++;
currA--;
}
else {
currC++;
currA++;
}
maxA = Math.max(maxA, currA);
maxC = Math.max(maxC, currC);
}
return Math.max(maxA, maxC);
}
let Str = "A?ACAC??A" ;
console.log(maximumdistance(Str));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...