Minimum moves to reach from i to j in a cyclic string

• Last Updated : 06 Jun, 2019

Given a cyclic string str and two integers i and j, the task is to count the minimum number of steps required to move from str[i] to str[j]. A move is to reach any adjacent character in the string and the move is only counted if str[start] != start[end] where start is the starting index for the move and end is the ending (adjacent either on the left or on the right) index. Since, the given string is circular, str and str[n – 1] are adjacent to each other.

Examples:

Input: str = “SSNSS”, i = 0, j = 3
Output: 0
From left to right : S -> S -> N -> S
From right to left : S -> S -> S

Input: str = “geeksforgeeks”, i = 0, j = 3
Output: 2

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Starting from index i start moving in the right direction till index j and for every character visited, if the current character is not equal to the previous character then increment steps1 = steps1 + 1.
• Similarly, starting from i start moving in the left direction till index 0 and for every character visited, if the current character is not equal to the previous character then increment steps2 = steps2 + 1. Once the index 0 is visited, start traversing from index n – 1 to j and increment step2 if str != str[n – 1].
• Print min(step1, step2) in the end.

Below is the implementation of the above approach:

C++

 // C++ implementation of the approach#include using namespace std;  // Function to return the count of steps// required to move from i to jint getSteps(string str, int i, int j, int n){    // Starting from i + 1    int k = i + 1;      // Count of steps    int steps = 0;      // Current character    char ch = str[i];    while (k <= j) {          // If current character is different from previous        if (str[k] != ch) {              // Increment steps            steps++;              // Update current character            ch = str[k];        }        k++;    }      // Return total steps    return steps;}  // Function to return the minimum number of steps// required to reach j from iint getMinSteps(string str, int i, int j, int n){      // Swap the values so that i <= j    if (j < i) {        int temp = i;        i = j;        j = temp;    }      // Steps to go from i to j (left to right)    int stepsToRight = getSteps(str, i, j, n);      // While going from i to j (right to left)    // First go from i to 0    // then from (n - 1) to j    int stepsToLeft = getSteps(str, 0, i, n)                      + getSteps(str, j, n - 1, n);      // If first and last character is different    // then it'll add a step to stepsToLeft    if (str != str[n - 1])        stepsToLeft++;      // Return the minimum of two paths    return min(stepsToLeft, stepsToRight);}  // Driver codeint main(){    string str = "SSNSS";    int n = str.length();    int i = 0, j = 3;    cout << getMinSteps(str, i, j, n);    return 0;}

Java

 // Java implementation of the approach  class GFG{    // Function to return the count of steps    // required to move from i to j    static int getSteps(String str, int i, int j, int n)    {        // Starting from i + 1        int k = i + 1;              // Count of steps        int steps = 0;              // Current character        char ch = str.charAt(i);        while (k <= j)         {                  // If current character is different from previous            if (str.charAt(k) != ch)            {                      // Increment steps                steps++;                      // Update current character                ch = str.charAt(k);            }            k++;        }              // Return total steps        return steps;    }          // Function to return the minimum number of steps    // required to reach j from i    static int getMinSteps(String str, int i, int j, int n)    {              // Swap the values so that i <= j        if (j < i)         {            int temp = i;            i = j;            j = temp;        }              // Steps to go from i to j (left to right)        int stepsToRight = getSteps(str, i, j, n);              // While going from i to j (right to left)        // First go from i to 0        // then from (n - 1) to j        int stepsToLeft = getSteps(str, 0, i, n)                        + getSteps(str, j, n - 1, n);              // If first and last character is different        // then it'll add a step to stepsToLeft        if (str.charAt(0) != str.charAt(n - 1))            stepsToLeft++;              // Return the minimum of two paths        return Math.min(stepsToLeft, stepsToRight);    }          // Driver code    public static void main(String []args)    {        String str = "SSNSS";        int n = str.length();        int i = 0, j = 3;        System.out.println(getMinSteps(str, i, j, n));    }}  // This code is contributed by ihritik

Python3

 # Python3 implementation of the approach  # Function to return the count of steps# required to move from i to jdef getSteps( str,  i, j, n) :      # Starting from i + 1    k = i + 1      # Count of steps    steps = 0      # Current character    ch = str[i]    while (k <= j):           # If current character is different from previous        if (str[k] != ch):               # Increment steps            steps = steps + 1              # Update current character            ch = str[k]                  k = k + 1            # Return total steps    return steps    # Function to return the minimum number of steps# required to reach j from idef getMinSteps( str, i, j, n):        # Swap the values so that i <= j    if (j < i):        temp = i        i = j        j = temp            # Steps to go from i to j (left to right)    stepsToRight = getSteps(str, i, j, n)      # While going from i to j (right to left)    # First go from i to 0    # then from (n - 1) to j    stepsToLeft = getSteps(str, 0, i, n) + getSteps(str, j, n - 1, n)      # If first and last character is different    # then it'll add a step to stepsToLeft    if (str != str[n - 1]):        stepsToLeft = stepsToLeft + 1      # Return the minimum of two paths    return min(stepsToLeft, stepsToRight)        # Driver code  str = "SSNSS"n = len(str)i = 0j = 3print(getMinSteps(str, i, j, n))  # This code is contributed by ihritik

C#

 // C# implementation of the approachusing System;  class GFG{    // Function to return the count of steps    // required to move from i to j    static int getSteps(string str, int i, int j, int n)    {        // Starting from i + 1        int k = i + 1;              // Count of steps        int steps = 0;              // Current character        char ch = str[i];        while (k <= j)        {                  // If current character is different from previous            if (str[k] != ch)            {                      // Increment steps                steps++;                      // Update current character                ch = str[k];            }            k++;        }              // Return total steps        return steps;    }          // Function to return the minimum number of steps    // required to reach j from i    static int getMinSteps(string str, int i, int j, int n)    {              // Swap the values so that i <= j        if (j < i)         {            int temp = i;            i = j;            j = temp;        }              // Steps to go from i to j (left to right)        int stepsToRight = getSteps(str, i, j, n);              // While going from i to j (right to left)        // First go from i to 0        // then from (n - 1) to j        int stepsToLeft = getSteps(str, 0, i, n)                        + getSteps(str, j, n - 1, n);              // If first and last character is different        // then it'll add a step to stepsToLeft        if (str != str[n - 1])            stepsToLeft++;              // Return the minimum of two paths        return Math.Min(stepsToLeft, stepsToRight);    }          // Driver code    public static void Main()    {        string str = "SSNSS";        int n = str.Length;        int i = 0, j = 3;        Console.WriteLine(getMinSteps(str, i, j, n));    }}  // This code is contributed by ihritik

PHP


Output:
0

My Personal Notes arrow_drop_up