Given a string str, containing upper case and lower case characters. In a single operations, any lowercase character can be converted to an uppercase character and vice versa. The task is to print the minimum number of such operations required so that the resultant string consists of zero or more upper case characters followed by zero or more lower case characters.
Input: str = “geEks”
Either the first 2 characters can be converted to uppercase characters i.e. “GEEks” with 2 operations.
Or the third character can be converted to a lowercase character i.e. “geeks” with a single operation.
Input: str = “geek”
The string is already in the specified format.
Approach: There are two possible cases:
- Find the index of the last uppercase character in the string and convert all the lowercase characters appearing before it into uppercase characters.
- Or, find the index of the first lowercase character in the string and convert all the uppercase characters appearing after it into lowercase characters.
Choose the case where the operations required are minimum.
Below is the implementation of the above approach:
Time Complexity: O(N) where N is the length of the string.
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.
- Lower case to upper case - An interesting fact
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Minimum move to end operations to make all strings equal
- Convert camel case string to snake case in Java
- Convert Snake Case string to Camel Case in Java
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
- Check if lowercase and uppercase characters are in same order
- Remove uppercase, lowercase, special, numeric, and non-numeric characters from a String
- Check if a string contains uppercase, lowercase, special characters and numeric values
- Find an index such that difference between product of elements before and after it is minimum
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Minimum operations to make frequency of all characters equal K
- Minimum operations required to convert all characters of a String to a given Character
- Convert characters of a string to opposite case
- Python | Toggle characters in words having same case
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Number of digits before the decimal point in the division of two numbers
- Count Uppercase, Lowercase, special character and numeric values
- First uppercase letter in a string (Iterative and Recursive)
- Maximum distinct lowercase alphabets between two uppercase
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.