Open In App

Find the minimum operations required to type the given String

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Geek is extremely punctual but today even he is not feeling like doing his homework assignment. He must start doing it immediately in order to meet the deadline. For the assignment, Geek needs to type a string s.
To reduce his workload, he has decided to perform one of the following two operations till he gets the string.

Append a character at the end of the string.
Append the string formed thus far to the end of the string. (This can not be done more than once.)
Help Geek find the minimum operations required to type the given string.

Examples:

Input: S = abcabca
Output: 5
Explanation: a -> ab -> abc -> abcabc -> abcabca

Input: S = abcdefgh
Output: 8
Explanation: a -> ab -> abc -> abcd -> abcde -> abcdef -> abcdefg -> abcdefgh

Approach: To solve the problem follow the below idea:

We will strive to employ the second operation carefully because it can only be performed once. As a result, we must use this method to create the longest string feasible. Select the rightmost index i such that s[0…i] = s[i+1…. 2i] to perform the second operation.

Below are the steps for the above approach:

  • Iterate the array from the midpoint i = n/2 to the starting index i = 0.
  • Check if the substring from index 0 to i is equal to the substring of the same length starting from i+1 for each iteration.
  • If two strings in any iteration are identical, perform the second procedure. In other words, perform the first operation till i, and then perform the second operation. Start adding a substring of the same length from position i+1 to complete the second operation.
  • Perform the first operation on the remaining parts because the second operation cannot be performed more than once.

Below is the implementation of the above approach:

Java




// Java code for the above approach:
import java.io.*;
import java.util.*;
 
class GFG {
    static int minOperation(String s)
    {
        int n = s.length();
        for (int i = n / 2 - 1; i >= 0; i--) {
            if (s.substring(0, i + 1).equals(
                    s.substring(i + 1, i + i + 2))) {
                return n - i;
            }
        }
        return n;
    }
 
    // Drivers code
    public static void main(String[] args)
    {
        String s = "abcabca";
        System.out.println(minOperation(s));
    }
}


Python3




# python code for the above approach:
def min_operation(s):
    n = len(s)
    for i in range(n // 2 - 1, -1, -1):
        if s[:i + 1] == s[i + 1: i + i + 2]:
            return n - i
    return n
 
# Drivers code
s = "abcabca"
print(min_operation(s))
 
# This code is contributed by prasad264


C#




// C# code for the above approach:
using System;
 
public class GFG {
    static int minOperation(string s)
    {
        int n = s.Length;
        for (int i = n / 2 - 1; i >= 0; i--) {
            if (s.Substring(0, i + 1) == s.Substring(i + 1, i + 1)) {
                return n - i;
            }
        }
        return n;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        string s = "abcabca";
        Console.WriteLine(minOperation(s));
    }
}
// This code is contributed by prasad264


Javascript




// JavaScript code for the above approach:
function min_operation(s) {
    let n = s.length;
    for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
        if (s.slice(0, i + 1) == s.slice(i + 1, i + i + 2)) {
            return n - i;
        }
    }
    return n;
}
 
// Driver code
let s = "abcabca";
console.log(min_operation(s));
 
// This code is contributed by prasad264


C++14




// C++ code:
 
#include <iostream>
using namespace std;
 
int minOperation(string s)
{
    int n = s.length();
    for (int i = n / 2 - 1; i >= 0; i--) {
        if (s.substr(0, i + 1) == s.substr(i + 1, i + 1)) {
            return n - i;
        }
    }
    return n;
}
 
int main()
{
 
    string s = "abcabca";
    cout << minOperation(s) << endl;
    return 0;
}


Output

5

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads