📜  循环字符串中从 i 到 j 的最小移动

📅  最后修改于: 2022-05-13 01:57:07.700000             🧑  作者: Mango

循环字符串中从 i 到 j 的最小移动

给定一个循环字符串str和两个整数ij ,任务是计算从str[i]移动到str[j]所需的最小步数。移动是到达字符串中的任何相邻字符,并且仅当str[start] != start[end]时才计算移动,其中start是移动的起始索引, end是结束(在左侧或左侧相邻)右)索引。由于给定的字符串是圆形的,因此str[0]str[n – 1]彼此相邻。

例子:

方法:

  • 从索引i开始朝正确的方向移动直到索引j并且对于访问的每个字符,如果当前字符不等于前一个字符,则递增steps1 = steps1 + 1
  • 类似地,从i开始向左移动直到索引0并且对于访问的每个字符,如果当前字符不等于前一个字符,则递增steps2 = steps2 + 1 。访问索引0后,开始从索引n – 1遍历到j ,如果str[0] != str[n – 1]则递增step2
  • 最后打印min(step1, step2)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of steps
// required to move from i to j
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
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[0] != str[n - 1])
        stepsToLeft++;
  
    // Return the minimum of two paths
    return min(stepsToLeft, stepsToRight);
}
  
// Driver code
int 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 j
def 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 i
def 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[0] != str[n - 1]):
        stepsToLeft = stepsToLeft + 1
  
    # Return the minimum of two paths
    return min(stepsToLeft, stepsToRight)
  
      
# Driver code
  
str = "SSNSS"
n = len(str)
i = 0
j = 3
print(getMinSteps(str, i, j, n))
  
# This code is contributed by ihritik


C#
// C# implementation of the approach
using 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[0] != 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


输出:
0