Open In App

Minimum number of Appends of X or Y characters from the end to the front required to obtain given string

Last Updated : 14 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string S and two positive integers X and Y, the task is to find the minimum number of operations required to obtain the original string. In each operation, append X or Y characters from the end of the string to the front of the string respectively in each operation.

Examples:

Input: S = "GeeksforGeeks", X = 5, Y = 3
Output: 3
Explanation:
Below are the operations performed:
Operation 1: Append 5 characters from the back of the string S to the front of the string S. Now the updated string is "GeeksGeeksfor".
Operation 2: Append 3 characters from the back of the string S to the front of the string S. Now the updated string is "forGeeksGeeks".
Operation 3: Append 5 characters from the back of the string S to the front of the string S. Now the updated string is "GeeksforGeeks".
Therefore, the minimum count of operations required is 3.

Input: S = "AbcDef", X = 1, Y = 2
Output: 4
Explanation:
Below are the operations performed:
Operation 1: Append 1 characters from the back of the string S to the front of the string S. Now the updated string is "fAbcDe".
Operation 2: Append 2 characters from the back of the string S to the front of the string S. Now the updated string is "fDeAbc".
Operation 3: Append 1 characters from the back of the string S to the front of the string S. Now the updated string is "cfDeAb".
Operation 4: Append 2 characters from the back of the string S to the front of the string S. Now the updated string is "AbcDef".
Therefore, the minimum count of operations required is 4.

 

Approach: The idea is to append X and Y characters from the given string to the front of the string respectively and keep the track of the count of operations while performing operations. Below are the steps:

  • Initialize the count as 0 that stores the minimum operations.
  • Store the given string S in another string(say newString) where operation needs to be performed.
  • Now perform the following operation on the newString till it is equal to S:
    • Append the X character from the end of the string newString and increment the count by 1.
    • If newString is the same as the string S.
    • Append the Y character from the end of the string newString and increment the count by 1.
    • If newString is the same as the string S.
  • After the above steps, print the value of the count as the minimum number of operations required.

Below is the implementation of the above approach:

C++
// C++ program for the above approach
#include <iostream>
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
JavaScript
<script>
    // Javascript 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
    function minimumOperations(orig_str, m, n)
    {

        // Store the original string
        let orig = orig_str;

        // Stores the count of operations
        let turn = 1;
        let j = 1;

        // Traverse the string
        for(let i = 0; i < orig_str.length; i++)
        {

            // Cut m letters from end
            let 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 != orig_str)
            {
                turn = turn + 1;

                // Cut n letters from end
                let 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 == orig_str)
            {
                break;
            }

            // Update the turn
            turn = turn + 1;
        }
        document.write(turn);
    }
    
    // Given string S
    let S = "GeeksforGeeks";
 
    let X = 5, Y = 3;
 
    // Function Call
    minimumOperations(S, X, Y);

// This code is contributed by vaibhavrabadiya117.
</script>

Output: 
3

 

Time Complexity: O(N)
Auxiliary Space: O(1)


Next Article

Similar Reads