Open In App

Circular movement in a Path

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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




// 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.


Python3




# 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#




// 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


Javascript




// 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)  



Last Updated : 17 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads