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:
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.
- Count of sub-sequences which satisfy the given condition
- Number of strings that satisfy the given condition
- Choose X elements from A and Y elements from B which satisfy the given condition
- Count of indices in an array that satisfy the given condition
- 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 given operations required to convert a string to another string
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Minimum given operations required to convert a given binary string to all 1's
- Minimize count of given operations required to make two given strings permutations of each other
- Minimum no. of operations required to make all Array Elements Zero
- Minimum number of operations required to make two strings equal
- Operations required to make the string empty
- Minimum number of operations required to obtain a given Binary String
- Minimum operations required to convert all characters of a String to a given Character
- Minimum changes required to make first string substring of second string
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum number of operations required to maximize the Binary String
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.