Open In App

Circular movement in a Path

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:

Below is the implementation of the code:




// C++ code for the above approach
#include <iostream>
#include <string>
using namespace std;
 
// Function to find the maximum distance
// between the number of 'A's and 'C's
// in a string
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 the current character is 'A'
        if (str[i] == 'A') {
 
            // Increment the number of
            // 'A's encountered so far
            currA++;
 
            // Decrement the number of
            // 'C's encountered so far
            currC--;
        }
 
        // If the current character
        // is 'C'
        else if (str[i] == 'C') {
 
            // Increment the number of
            // 'C's encountered so far
            currC++;
 
            // Decrement the number of
            // 'A's encountered so far
            currA--;
        }
 
        // If the current character is
        // neither 'A' nor 'C' (i.e., '?')
        else {
 
            // Increment the number of
            // 'C's encountered so far
            currC++;
 
            // Increment the number of
            // 'A's encountered so far
            currA++;
        }
 
        // Update maxA if currA is
        // greater than maxA
        maxA = max(maxA, currA);
 
        // Update maxC if currC is
        // greater than maxC
        maxC = max(maxC, currC);
    }
    return max(maxA, maxC);
}
 
// Driver code
int main()
{
    string Str = "A?ACAC??A";
 
    // Function call
    cout << maximumdistance(Str);
    return 0;
}




// Java code for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the maximum distance between the
    // number of 'A's and 'C's in a string
    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 the current character is 'A'
            if (str.charAt(i) == 'A') {
                // Increment the number of 'A's encountered
                // so far
                currA++;
 
                // Decrement the number of 'C's encountered
                // so far
                currC--;
            }
 
            // If the current character is 'C'
            else if (str.charAt(i) == 'C') {
                // Increment the number of 'C's encountered
                // so far
                currC++;
 
                // Decrement the number of 'A's encountered
                // so far
                currA--;
            }
 
            // If the current character is neither 'A' nor
            // 'C' (i.e., '?')
            else {
                // Increment the number of 'C's encountered
                // so far
                currC++;
 
                // Increment the number of 'A's encountered
                // so far
                currA++;
            }
 
            // Update maxA if currA is greater than maxA
            maxA = Math.max(maxA, currA);
 
            // Update maxC if currC is greater than maxC
            maxC = Math.max(maxC, currC);
        }
        return Math.max(maxA, maxC);
    }
 
    public static void main(String[] args)
    {
        String Str = "A?ACAC??A";
 
        // Function call
        System.out.println(maximumdistance(Str));
    }
}
 
// This code is contributed by karthik.




# Function to find the maximum distance
# between the number of 'A's and 'C's
# in a string
def maximumdistance(s: str) -> int:
    N = len(s)
    currA = 0
    currC = 0
    maxA = 0
    maxC = 0
 
    for i in range(N):
        # If the current character is 'A'
        if s[i] == 'A':
            # Increment the number of 'A's encountered so far
            currA += 1
            # Decrement the number of 'C's encountered so far
            currC -= 1
        # If the current character is 'C'
        elif s[i] == 'C':
            # Increment the number of 'C's encountered so far
            currC += 1
            # Decrement the number of 'A's encountered so far
            currA -= 1
        # If the current character is neither 'A' nor 'C' (i.e., '?')
        else:
            # Increment the number of 'C's encountered so far
            currC += 1
            # Increment the number of 'A's encountered so far
            currA += 1
 
        # Update maxA if currA is greater than maxA
        maxA = max(maxA, currA)
        # Update maxC if currC is greater than maxC
        maxC = max(maxC, currC)
 
    return max(maxA, maxC)
 
# Drive code
if __name__ == '__main__':
    Str = "A?ACAC??A"
 
    # Function call
    print(maximumdistance(Str))
 
  # This code is contributed by Nikhil Saini




// C# code for the above approach
using System;
 
class GFG {
 
    // Function to find the maximum distance between the
    // number of 'A's and 'C's in a string
    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 the current character is 'A'
            if (str[i] == 'A') {
                // Increment the number of 'A's encountered
                // so far
                currA++;
 
                // Decrement the number of 'C's encountered
                // so far
                currC--;
            }
 
            // If the current character is 'C'
            else if (str[i] == 'C') {
                // Increment the number of 'C's encountered
                // so far
                currC++;
 
                // Decrement the number of 'A's encountered
                // so far
                currA--;
            }
 
            // If the current character is neither 'A' nor
            // 'C' (i.e., '?')
            else {
                // Increment the number of 'C's encountered
                // so far
                currC++;
 
                // Increment the number of 'A's encountered
                // so far
                currA++;
            }
 
            // Update maxA if currA is greater than maxA
            maxA = Math.Max(maxA, currA);
 
            // Update maxC if currC is greater than maxC
            maxC = Math.Max(maxC, currC);
        }
        return Math.Max(maxA, maxC);
    }
 
    public static void Main()
    {
        string Str = "A?ACAC??A";
 
        // Function call
        Console.WriteLine(maximumdistance(Str));
    }
}
 
// This code is contributed by Vaibhav Nandan




// Function to find the maximum distance
// between the number of 'A's and 'C's
// in a string
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 the current character is 'A'
        if (str[i] === 'A') {
         
            // Increment the number of
            // 'A's encountered so far
            currA++;
             
            // Decrement the number of
            // 'C's encountered so far
            currC--;
        }
        // If the current character
        // is 'C'
        else if (str[i] === 'C') {
         
            // Increment the number of
            // 'C's encountered so far
            currC++;
             
            // Decrement the number of
            // 'A's encountered so far
            currA--;
        }
        // If the current character is
        // neither 'A' nor 'C' (i.e., '?')
        else {
         
            // Increment the number of
            // 'C's encountered so far
            currC++;
             
            // Increment the number of
            // 'A's encountered so far
            currA++;
        }
         
        // Update maxA if currA is
        // greater than maxA
        maxA = Math.max(maxA, currA);
         
        // Update maxC if currC is
        // greater than maxC
        maxC = Math.max(maxC, currC);
    }
    return Math.max(maxA, maxC);
}
// Driver code
let Str = "A?ACAC??A";
console.log(maximumdistance(Str));

Output
5



Time Complexity: O(N) 
Auxiliary Space: O(1)  


Article Tags :