Given a string str consisting of uppercase and lowercase characters. The task is to sort uppercase and lowercase characters separately such that if the ith place in the original string had an uppercase character then it should not have a lowercase character after being sorted and vice versa.
Input: str = “gEeksfOrgEEkS”
Input: str = “eDefSR”
Approach: Take two arrays lower and upper to store the frequencies of all the lowercase and the uppercase characters in the given string then traverse the string once again and for every lowercase character encountered, replace it with the smallest lowercase character available using the lower array and replace the uppercase characters using the upper array.
Below is the implementation of the above approach:
Time Complexity: O(n)
Space Complexity: O(1)
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.
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Sorting objects using In-Place sorting algorithm
- Sorting a Queue without extra space
- Sorting integer data from file and calculate execution time
- Time Complexities of all Sorting Algorithms
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
- Merge Sort with O(1) extra space merge and O(n lg n) time
- Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space
- Sorting Strings using Bubble Sort
- Sorting array of strings (or words) using Trie
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
- Case-specific Sorting of Strings
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Find the time which is palindromic and comes after the given time
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- Asymptotic Analysis and comparison of sorting algorithms
- Merging and Sorting Two Unsorted Stacks
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.