Given a string S, we need to write a program to check if it is possible to construct the given string S by performing any of the below operations any number of times. In each step, we can:
- Add any character at the end of the string.
- or, append the string to the string itself.
The above steps can be applied any number of times. We need to write a program to print the minimum steps required to form the string. Examples:
Input : aaaaaaaa
Output : 4
Explanation: move 1: add 'a' to form "a"
move 2: add 'a' to form "aa"
move 3: append "aa" to form "aaaa"
move 4: append "aaaa" to form "aaaaaaaa"
Input: aaaaaa
Output: 4
Explanation: move 1: add 'a' to form "a"
move 2: add 'a' to form "aa"
move 3: add 'a' to form "aaa"
move 4: append "aaa" to form "aaaaaa"
Input: abcabca
Output: 5
The idea to solve this problem is to use Dynamic Programming to count the minimum number of moves. Create an array named dp of size n, where n is the length of the input string. dp[i] stores the minimum number of moves that are required to make substring (0…i). According to the question there are two moves that are possible:
- dp[i] = min(dp[i], dp[i-1] + 1) which signifies addition of characters.
- dp[i*2+1] = min(dp[i]+1, dp[i*2+1]), appending of string is done if s[0…i]==s[i+1..i*2+1]
The answer will be stored in dp[n-1] as we need to form the string(0..n-1) index-wise.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int minimalSteps(string s, int n)
{
int dp[n];
for ( int i = 0; i < n; i++)
dp[i] = INT_MAX;
string s1 = "" , s2 = "" ;
dp[0] = 1;
s1 += s[0];
for ( int i = 1; i < n; i++) {
s1 += s[i];
s2 = s.substr(i + 1, i + 1);
dp[i] = min(dp[i], dp[i - 1] + 1);
if (s1 == s2)
dp[i * 2 + 1] = min(dp[i] + 1, dp[i * 2 + 1]);
}
return dp[n - 1];
}
int main()
{
string s = "aaaaaaaa" ;
int n = s.length();
cout << minimalSteps(s, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minimalSteps(String s, int n)
{
int []dp = new int [n];
for ( int i = 0 ; i < n; i++)
dp[i] = Integer.MAX_VALUE;
String s1 = "" , s2 = "" ;
dp[ 0 ] = 1 ;
s1 += s.charAt( 0 );
for ( int i = 1 ; i < n; i++)
{
s1 += s.charAt(i);
s2 = s.substring(i + 1 , i + 1 );
dp[i] = Math.min(dp[i], dp[i - 1 ] + 1 );
if (s1 == s2)
dp[i * 2 + 1 ] = Math.min(dp[i] + 1 ,
dp[i * 2 + 1 ]);
}
return dp[n - 1 ];
}
public static void main(String args[])
{
String s = "aaaaaaaa" ;
int n = s.length();
System.out.println(minimalSteps(s, n)/ 2 );
}
}
|
Python3
INT_MAX = 100000000
def minimalSteps(s, n):
dp = [INT_MAX for i in range (n)]
s1 = ""
s2 = ""
dp[ 0 ] = 1
s1 + = s[ 0 ]
for i in range ( 1 , n):
s1 + = s[i]
s2 = s[i + 1 : i + 1 + i + 1 ]
dp[i] = min (dp[i], dp[i - 1 ] + 1 )
if (s1 = = s2):
dp[i * 2 + 1 ] = min (dp[i] + 1 ,
dp[i * 2 + 1 ])
return dp[n - 1 ]
s = "aaaaaaaa"
n = len (s)
print ( minimalSteps(s, n) )
|
C#
using System;
class GFG
{
static int minimalSteps(String s, int n)
{
int []dp = new int [n];
for ( int i = 0; i < n; i++)
dp[i] = int .MaxValue;
String s1 = "" , s2 = "" ;
dp[0] = 1;
s1 += s[0];
for ( int i = 1; i < n; i++)
{
s1 += s[i];
s2 = s.Substring(i , 1);
dp[i] = Math.Min(dp[i], dp[i - 1] + 1);
if (s1 == s2)
dp[i * 2 + 1] = Math.Min(dp[i] + 1,
dp[i * 2 + 1]);
}
return dp[n - 1];
}
public static void Main(String []args)
{
String s = "aaaaaaaa" ;
int n = s.Length;
Console.Write(minimalSteps(s, n)/2);
}
}
|
PHP
<?php
function minimalSteps( $s , $n )
{
for ( $i = 0; $i < $n ; $i ++)
$dp [ $i ] = PHP_INT_MAX;
$s1 = "" ;
$s2 = "" ;
$dp [0] = 1;
$s1 = $s1 . $s [0];
for ( $i = 1; $i < $n ; $i ++)
{
$s1 = $s1 . $s [ $i ];
$s2 = substr ( $s , $i + 1, $i + 1);
$dp [ $i ] = min( $dp [ $i ],
$dp [ $i - 1] + 1);
if ( $s1 == $s2 )
$dp [ $i * 2 + 1] = min( $dp [ $i ] + 1,
$dp [ $i * 2 + 1]);
}
return $dp [ $n - 1];
}
$s = "aaaaaaaa" ;
$n = strlen ( $s );
echo minimalSteps( $s , $n );
?>
|
Javascript
function minimalSteps(s, n)
{
let dp = [n]
for (let i = 0; i < n; i++)
dp[i] = Number.MAX_SAFE_INTEGER
let s1 = ""
let s2 = ""
dp[0] = 1
s1 += s[0]
for (let i = 1; i < n; i++) {
s1 += s[i]
s2 = s.substr(i + 1, i + 1);
dp[i] = Math.min(dp[i], dp[i - 1] + 1);
if (s1 == s2)
dp[i * 2 + 1] = Math.min(dp[i] + 1, dp[i * 2 + 1])
}
return dp[n - 1]
}
let s = "aaaaaaaa"
let n = s.length
console.log(minimalSteps(s, n))
|
Time Complexity: O(n2), where n is the length of the input string.
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!