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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum no. of operations required to make all Array Elements Zero
- Minimum changes required such that the string satisfies the given condition
- Operations required to make the string empty
- Minimum number of operations required to make two strings equal
- Minimum number of given operations required to make two strings equal
- 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 alternating
- Minimum number of swaps required to make the string K periodic
- Minimum swaps required to make a binary string divisible by 2^k
- 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
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Count of sub-sequences which satisfy the given condition
- Number of strings that satisfy the given condition
- Minimum number of pairs required to make two strings same
- Minimum characters required to make a password strong
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.