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.
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]
- Replace minimal number of characters to make all characters pair wise distinct
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically largest string possible for a given cost of appending characters
- Smallest window that contains all characters of string itself
- Minimal operations to make a number magical
- Make a lexicographically smallest palindrome with minimal changes
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Check if a string can be obtained by appending subsequences of another string
- Check if string S2 can be obtained by appending subsequences of string S1
- Find the character made by adding all the characters of the given string
- Count of elements having Euler's Totient value one less than itself
- Count of valid pairs (X, Y) from given strings such that concatenating X with itself yields Y
- Minimum cost to form a number X by adding up powers of 2
- Minimum moves to reach from i to j in a cyclic string
- Check if characters of a given string can be rearranged to form a palindrome
- Check if characters of one string can be swapped to form other
- Check if the characters in a string form a Palindrome in O(1) extra space
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Check if characters of a given string can be used to form any N equal strings
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 above idea:
Time Complexity : O(n2), where n is the length of input string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.