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.
- Get first and last elements from Array and Vector in CPP
- Lexicographically next greater string using same character set
- Minimum changes required to make all Array elements Prime
- Last element of vector in C++ (Accessing and updating)
- Move matrix elements in given direction and add elements with same value
- Program to print last N lines | Set-2
- Make three non-empty sets with negative, positive and 0 products
- Check if two BSTs contain same set of elements
- Check if two expressions with brackets are same
- Maximum array from two given arrays keeping order same
- Traversal of tree with k jumps allowed between nodes of same height
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Print the arranged positions of characters to make palindrome
- Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
- Breadth First Search without using Queue
- How to find the minimum and maximum element of a Vector using STL in C++?
- Minimum cost to process m tasks where switching costs
- Minimum steps in which N can be obtained using addition or subtraction at every step
- Minimum cost path from source node to destination node via an intermediate node
- Minimum number of days required to complete the work
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.