Maximum moves to reach destination character in a cyclic String
Given a cyclic string S of length N consisting of only three characters ‘a‘, ‘b‘, and ‘c‘. Also given two characters, initial character (ic) and final character (fc). the task is to find the maximum distance from ic to its closest fc.
Examples:
Input: s = “caacb”, ic = ‘c’, fc = ‘a’
Output: 3
Explanation: The initial character is ‘c’ and final character is ‘a’, so there are two options: either ‘a’ will be on just after 1 move(if we choose s[0] as ic), or after 3 moves (if we choose s[3] as ic). So, the answer is equal to 3 — which is the maximum possible number of moves to reach from ic to fc.Input: s = “cccabbbab”, ic = ‘b’, fc = ‘a’
Output: 4
Approach: To solve the problem follow the below idea:
For each traverse of ic in the string, we need to find the rightmost fc distance, and then find the maximum distance between ic and the nearest fc.
Follow the steps to solve the problem:
- To get rid of cyclicity, we can write the string S twice and for each cell of ic from the first half, find the nearest fc on the right, thus we solved the problem with cyclicity.
- And now we can just follow this line from right to left and maintain the index of the last occurrence of fc.
- If we encounter ic next, then update the answer as ans = max(ans, last – i).
- Where last is the nearest one that fc was traversed, and i is the current one.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Maximum possible moves to reach destination int maxMoves(string s, char ic, char fc) { // Length of the string int n = s.size(); // To get rid of cyclicity s += s; // To store the answer int ans = 0, f = -1; // Corner Case if (ic == fc) { return 0; } // Traversing loop from backwards // and calculate accordingly for ( int i = 2 * n - 1; i >= 0; i--) { if (s[i] == ic && f != -1) { ans = max(ans, f - i); } if (s[i] == fc) { f = i; } } // Return the answer return ans; } // Driver Code int main() { string s = "caacb" ; char ic = 'c' , fc = 'a' ; // Function Call cout << maxMoves(s, ic, fc); return 0; } |
Java
// Java code to implement the approach import java.io.*; class GFG { // Maximum possible moves to reach destination static int maxMoves(String s, char ic, char fc) { // Length of the string int n = s.length(); // To get rid of cyclicity s += s; // To store the answer int ans = 0 , f = - 1 ; // Corner Case if (ic == fc) { return 0 ; } // Traversing loop from backwards // and calculate accordingly for ( int i = 2 * n - 1 ; i >= 0 ; i--) { if (s.charAt(i) == ic && f != - 1 ) { ans = Math.max(ans, f - i); } if (s.charAt(i) == fc) { f = i; } } // Return the answer return ans; } public static void main(String[] args) { String s = "caacb" ; char ic = 'c' , fc = 'a' ; // Function call System.out.print(maxMoves(s, ic, fc)); } } // This code is contributed by lokeshmvs21. |
Python3
# Python code to implement the approach # Maximum possible moves to reach destination def maxMoves(s, ic, fc): # Length of the string n = len (s) # To get rid of cyclicity s + = s # To store the answer ans, f = 0 , - 1 # corner case if (ic = = fc): return 0 # Traversing loop from backwards # calculate accordingly for i in range ( 2 * n - 1 , - 1 , - 1 ): if (s[i] = = ic and f is not - 1 ): ans = max (ans, f - i) if (s[i] = = fc): f = i # return the answer return ans s = "caacb" ic = 'c' fc = 'a' # Function call print (maxMoves(s, ic, fc)) # This code is contributed by lokesh |
C#
using System; public class GFG { // Maximum possible moves to reach destination public static int maxMoves( string s, char ic, char fc) { // Length of the string int n = s.Length; // To get rid of cyclicity s += s; // To store the answer int ans = 0, f = -1; // Corner Case if (ic == fc) { return 0; } // Traversing loop from backwards // and calculate accordingly for ( int i = 2 * n - 1; i >= 0; i--) { if (s[i] == ic && f != -1) { ans = Math.Max(ans, f - i); } if (s[i] == fc) { f = i; } } // Return the answer return ans; } static public void Main() { string s = "caacb" ; char ic = 'c' , fc = 'a' ; // Function Call Console.WriteLine(maxMoves(s, ic, fc)); } } // This code is contributed by ksam24000. |
Javascript
// JS code to implement the approach // Maximum possible moves to reach destination function maxMoves( s, ic, fc) { // Length of the string let n = s.length; // To get rid of cyclicity s += s; // To store the answer let ans = 0, f = -1; // Corner Case if (ic == fc) { return 0; } // Traversing loop from backwards // and calculate accordingly for (let i = 2 * n - 1; i >= 0; i--) { if (s[i] == ic && f != -1) { ans = Math.max(ans, f - i); } if (s[i] == fc) { f = i; } } // Return the answer return ans; } // Driver Code let s = "caacb" ; let ic = 'c' let fc = 'a' ; // Function Call console.log(maxMoves(s, ic, fc)); //This code is contributed by ksam24000 |
3
Time Complexity: O(N)
Auxiliary Space: O(N) as we are concatenating the same string to itself
Related Articles:
Please Login to comment...