- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum Number of Operations to move all Uppercase Characters before all Lower Case Characters
You are given a string 'str' that contains both uppercase and lowercase letters. Any lowercase character can be changed to an uppercase character and vice versa in a single action. The goal is to print the least possible instances of this process that are necessary to produce a string containing at least one lowercase character, followed by at least one uppercase character.
Input Output Scenarios
First possible solution: the first 4 characters can be converted to uppercase characters i.e. “TUTORial” with 4 operations.
Input
str = “tutoRial”
Output
1
Second possible solution: the third character can be converted to a lowercase character i.e. “tutorial” with a single operation.
Input
str = “point”
Output
0
The string is already in the specified format.
There are two possible outcomes:
The string's lowercase characters should all be converted to uppercase ones by determining the index of the last uppercase character.
Alternately, get the index of the first lowercase character in the string and convert all the capital letters that come after it to lowercase.
Choose the situation where there are the fewest possible tasks.
Java implementation
Let’s look into the Java implementation of this approach:
Example
public class Main { public static int minOperationsLower(String str, int n) { // Store the indices of the last uppercase character and the first lowercase character int i, lastUpperCase = -1, firstLowerCase = -1; for (i = n - 1; i >= 0; i--) { //finding uppercase if (Character.isUpperCase(str.charAt(i))) { lastUpperCase = i; break; } } for (i = 0; i < n; i++) { //finding lowercase if (Character.isLowerCase(str.charAt(i))) { firstLowerCase = i; break; } } if (lastUpperCase == -1 || firstLowerCase == -1) return 0; // Counting of uppercase characters that appear after the first lowercase character int countUpperCase = 0; for (i = firstLowerCase; i < n; i++) { if (Character.isUpperCase(str.charAt(i))) { countUpperCase++; } } // Count of lowercase characters that appear before the last uppercase character int countLowerCase = 0; for (i = 0; i < lastUpperCase; i++) { if (Character.isLowerCase(str.charAt(i))) { countLowerCase++; } } // Return the minimum operations required return Math.min(countLowerCase, countUpperCase); } // main method public static void main(String args[]) { String str = "tutoRIalsPoinT"; int n = str.length(); System.out.println("Operations Required: "+minOperationsLower(str, n)); } }
Output
Operations Required: 4
Time Complexity: O(N), where N is length of the string
Space Complexity: O(1), no extra space used.