Given a string str, the task is to make the string start and end at the same character with the minimum number of given operations. In a single operation, any character of the string can be removed. Note that the length of the resultant string must be greater than 1 and it is not possible then print -1.
Input: str = “geeksforgeeks”
Remove the first and the last two characters
and the string becomes “eeksforgee”
Input: str = “abcda”
Approach: If the string needs to start and end at a character say ch then an optimal way is to remove all the characters before the first occurrence of ch and all the characters after the last occurrence of ch. Find the number of characters that need to be removed for every possible value of ch starting from ‘a’ to ‘z’ and choose the one with the minimum number of delete operations.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum no. of operations required to make all Array Elements Zero
- Minimum changes required such that the string satisfies the given condition
- Minimum number of given operations required to make two strings equal
- Minimum number of operations required to make two strings equal
- Operations required to make the string empty
- Minimum number of given operations required to convert a string to another string
- Minimum given operations required to convert a given binary string to all 1's
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum changes required to make first string substring of second string
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum swaps required to make a binary string alternating
- Minimum number of swaps required to make the string K periodic
- Find the number of operations required to make all array elements Equal
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Minimum number of operations required to reduce N to 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.