Open In App

Find the minimum operations required to type the given String

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:

Below is the implementation of the above approach:




// 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));
    }
}




# 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# 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 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++ 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)


Article Tags :