Maximum moves to reach destination character in a cyclic String
Last Updated :
02 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int maxMoves(string s, char ic, char fc)
{
int n = s.size();
s += s;
int ans = 0, f = -1;
if (ic == fc) {
return 0;
}
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 ans;
}
int main()
{
string s = "caacb" ;
char ic = 'c' , fc = 'a' ;
cout << maxMoves(s, ic, fc);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxMoves(String s, char ic, char fc)
{
int n = s.length();
s += s;
int ans = 0 , f = - 1 ;
if (ic == fc) {
return 0 ;
}
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 ans;
}
public static void main(String[] args)
{
String s = "caacb" ;
char ic = 'c' , fc = 'a' ;
System.out.print(maxMoves(s, ic, fc));
}
}
|
Python3
def maxMoves(s, ic, fc):
n = len (s)
s + = s
ans, f = 0 , - 1
if (ic = = fc):
return 0
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 ans
s = "caacb"
ic = 'c'
fc = 'a'
print (maxMoves(s, ic, fc))
|
C#
using System;
public class GFG {
public static int maxMoves( string s, char ic, char fc)
{
int n = s.Length;
s += s;
int ans = 0, f = -1;
if (ic == fc) {
return 0;
}
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 ans;
}
static public void Main()
{
string s = "caacb" ;
char ic = 'c' , fc = 'a' ;
Console.WriteLine(maxMoves(s, ic, fc));
}
}
|
Javascript
function maxMoves( s, ic, fc)
{
let n = s.length;
s += s;
let ans = 0, f = -1;
if (ic == fc) {
return 0;
}
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 ans;
}
let s = "caacb" ;
let ic = 'c'
let fc = 'a' ;
console.log(maxMoves(s, ic, fc));
|
Time Complexity: O(N)
Auxiliary Space: O(N) as we are concatenating the same string to itself
Related Articles:
Share your thoughts in the comments
Please Login to comment...