Given a string S. You are allowed two types of operations:
- Remove a character from the front of the string.
- Remove a character from the end of the string.
The task is to find the minimum operations required to make the first and last character of the S same. In case, it is not possible, print “-1”.
Input : S = "bacdefghipalop" Output : 4 Remove 'b' from the front and remove 'p', 'o', 'l' from the end of the string S. Input : S = "pqr" Output : -1
Recursive: Call a recursive function passing four arguments string, starting index, ending index and count of the number of eliminations still now.
Below is the implementation of the above approach:
The idea is to prevent making further recursive calls for count>= Min once we found the Min during every recursive call. It saves a lot of time and is almost comparable to tabulation method in terms of time complexity.
Dynamic programming with Tabulation:
The idea is to find the first and last occurrences of each character in the string. The total amount of operations needed will be simply “number of operations needed to remove the first occurrence” plus “number of operations needed to remove the last occurrence”. So, do this for each character in the string and the answer will be minimum of such operations performed on each character.
For example, S = “zabcdefghaabbbb”, calculate the operations required to have character ‘a’ at both the front and the end, meaning to say the string “a….a”. For the minimum number of operations, we will form the string “abcdefghaa” i.e we will remove one character ‘z’ from front and 4 characters ‘bbbb’ from back. Hence total 5 operations will be required.
So, apply the above algorithm for each character and hence we can then find the minimum of those operations.
Below is implementation of this approach:
Time Complexity : O(n)
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.
- Minimum addition/removal of characters to be done to make frequency of each character prime
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Make the string in AP by changing a character
- Remove a character from a string to make it a palindrome
- Number of character corrections in the given strings to make them equal
- Transform One String to Another using Minimum Number of Given Operation
- Reduce the string to minimum length with the given operation
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Print the final string when minimum value strings get concatenated in every operation
- Minimum K such that every substring of length atleast K contains a character c
- Minimum cost to make two strings same
- Minimum sum of squares of character counts in a given string after removing k characters
- Minimum removal to make palindrome permutation
- Minimum changes to a string to make all substrings distinct
- Minimum Cost To Make Two Strings Identical
- Minimum operations to make frequency of all characters equal K
- Count minimum swap to make string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of Parentheses to be added to make it valid