📌  相关文章
📜  获得给定字符串所需的从头到尾的X或Y字符追加的最小数量

📅  最后修改于: 2021-04-22 02:37:51             🧑  作者: Mango

给定字符串S和两个正整数XY ,任务是找到获得原始字符串所需的最少操作数。在每一个操作中,来自字符串到所述字符串的分别在每次操作中的前部的端部追加的XY的字符。

例子:

方法:想法是将给定字符串中的XY字符附加到字符串的开头,并在执行操作时跟踪操作计数。步骤如下:

  • 将计数初始化为0 ,以存储最少的操作。
  • 将给定的字符串S存储在需要执行操作的另一个字符串(例如newString )中。
  • 现在,在newString上执行以下操作,直到它等于S为止:
    • 从字符串newString的末尾追加X字符,并将计数增加1
    • 如果newString与字符串S相同。
    • 从字符串newString的末尾追加Y字符,并将计数增加1
    • 如果newString与字符串S相同。
  • 完成上述步骤后,将计数值打印为所需的最少操作数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum operations
// required to get the given string after
// appending m or n characters from the end
// to the front of the string in each operation
int minimumOperations(string orig_str, int m, int n)
{
     
    // Store the original string
    string orig = orig_str;
     
    // Stores the count of operations
    int turn = 1;
    int j = 1;
 
    // Traverse the string
    for(auto i : orig_str)
    {
         
        // Cut m letters from end
        string m_cut = orig_str.substr(
            orig_str.length() - m);
 
        orig_str.erase(orig_str.length() - m);
 
        // Add cut m letters to beginning
        orig_str = m_cut + orig_str;
 
        // Update j
        j = j + 1;
 
        // Check if strings are the same
        if (orig != orig_str)
        {
            turn = turn + 1;
 
            // Cut n letters from end
            string n_cut = orig_str.substr(
                orig_str.length() - n);
            orig_str.erase(orig_str.length() - n);
 
            // Add cut n letters to beginning
            orig_str = n_cut + orig_str;
 
            // Update j
            j = j + 1;
        }
 
        // Check if strings are the same
        if (orig == orig_str)
        {
            break;
        }
 
        // Update the turn
        turn = turn + 1;
    }
    cout << turn;
}
 
// Driver Code
int main()
{
     
    // Given string S
    string S = "GeeksforGeeks";
 
    int X = 5, Y = 3;
 
    // Function Call
    minimumOperations(S, X, Y);
    return 0;
}
 
// This code is contributed by akhilsaini


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the minimum operations
// required to get the given string after
// appending m or n characters from the end
// to the front of the string in each operation
static void minimumOperations(String orig_str,
                              int m, int n)
{
     
    // Store the original string
    String orig = orig_str;
 
    // Stores the count of operations
    int turn = 1;
    int j = 1;
 
    // Traverse the string
    for(int i = 0; i < orig_str.length(); i++)
    {
         
        // Cut m letters from end
        String m_cut = orig_str.substring(
            orig_str.length() - m);
 
        orig_str = orig_str.substring(
            0, orig_str.length() - m);
 
        // Add cut m letters to beginning
        orig_str = m_cut + orig_str;
 
        // Update j
        j = j + 1;
 
        // Check if strings are the same
        if (!orig.equals(orig_str))
        {
            turn = turn + 1;
 
            // Cut n letters from end
            String n_cut = orig_str.substring(
                orig_str.length() - n);
            orig_str = orig_str.substring(
                0, orig_str.length() - n);
 
            // Add cut n letters to beginning
            orig_str = n_cut + orig_str;
 
            // Update j
            j = j + 1;
        }
 
        // Check if strings are the same
        if (orig.equals(orig_str))
        {
            break;
        }
 
        // Update the turn
        turn = turn + 1;
    }
    System.out.println( turn );
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given string S
    String S = "GeeksforGeeks";
 
    int X = 5, Y = 3;
 
    // Function Call
    minimumOperations(S, X, Y);
}
}
 
// This code is contributed by akhilsaini


Python
# Python program for the above approach
 
# Function to find the minimum operations
# required to get the given string after
# appending m or n characters from the end
# to the front of the string in each operation
def minimumOperations(orig_str, m, n):
 
    # Store the original string
    orig = orig_str
     
    # Stores the count of operations
    turn = 1
    j = 1
     
    # Traverse the string
    for i in orig_str:
 
        # Cut m letters from end
        m_cut = orig_str[-m:]
         
        orig_str = orig_str.replace(' ', '')[:-m]
 
        # Add cut m letters to beginning
        orig_str = m_cut + orig_str
 
        # Update j
        j = j + 1
 
        # Check if strings are the same
        if orig != orig_str:
            turn = turn + 1
 
            # Cut n letters from end
            n_cut = orig_str[-n:]
            orig_str = orig_str.replace(' ', '')[:-n]
 
            # Add cut n letters to beginning
            orig_str = n_cut + orig_str
     
            # Update j
            j = j + 1
 
        # Check if strings are the same
        if orig == orig_str:
            break
 
        # Update the turn
        turn = turn + 1
 
    print(turn)
 
 
# Driver Code
 
# Given string S
S = "GeeksforGeeks"
 
X = 5
Y = 3
 
# Function Call
minimumOperations(S, X, Y)


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum operations
// required to get the given string after
// appending m or n characters from the end
// to the front of the string in each operation
static void minimumOperations(string orig_str, int m,
                              int n)
{
     
    // Store the original string
    string orig = orig_str;
 
    // Stores the count of operations
    int turn = 1;
    int j = 1;
 
    // Traverse the string
    for(int i = 0; i < orig_str.Length; i++)
    {
         
        // Cut m letters from end
        string m_cut = orig_str.Substring(
            orig_str.Length - m);
 
        orig_str = orig_str.Substring(
            0, orig_str.Length - m);
 
        // Add cut m letters to beginning
        orig_str = m_cut + orig_str;
 
        // Update j
        j = j + 1;
 
        // Check if strings are the same
        if (!orig.Equals(orig_str))
        {
            turn = turn + 1;
 
            // Cut n letters from end
            String n_cut = orig_str.Substring(
                orig_str.Length - n);
            orig_str = orig_str.Substring(
                0, orig_str.Length - n);
 
            // Add cut n letters to beginning
            orig_str = n_cut + orig_str;
 
            // Update j
            j = j + 1;
        }
 
        // Check if strings are the same
        if (orig.Equals(orig_str))
        {
            break;
        }
 
        // Update the turn
        turn = turn + 1;
    }
    Console.WriteLine(turn);
}
 
// Driver Code
public static void Main()
{
     
    // Given string S
    string S = "GeeksforGeeks";
 
    int X = 5, Y = 3;
 
    // Function Call
    minimumOperations(S, X, Y);
}
}
 
// This code is contributed by akhilsaini


输出:
3



时间复杂度: O(N)
辅助空间: O(1)