Alternate Lower Upper String Sort in C++



We are given a string that contains both lowercase and uppercase characters, and we have to sort them in an alternate way, meaning one lowercase letter, then one uppercase letter, then again a lowercase letter, and so on, all in sorted order within their cases.

Let's understand this with a few example scenarios.

Scenario 1

Input: "aFegrAfStRzsV"
Output: "AagfRsSeTvz"
Explanation:
Sorted uppercase letters: A, F, R, S, T, V
Sorted lowercase letters: a, e, f,  g, r, s, z
We place one uppercase letter, then one lowercase letter, starting with an uppercase. 
We repeat this until all letters are used.

Scenario 2

Input: "QWErTYUiOP"
Output: "OePiQrTUWY"
Explanation:
Sorted uppercase letters: O, P, Q, T, U, W, Y
Sorted lowercase letters: e, i, r
Since the number of lowercase and uppercase letters may not be the same.
We keep placing one uppercase and one lowercase letter alternately until all are used.

Alternate Upper-Lower Case Letters

To sort the string in an alternate upper and lowercase order, we use two arrays. We first separate the uppercase and lowercase characters from the original string and sort them. Then, we take characters in an alternating way from both the sorted parts.

The following are the steps to do this:

  • We create two frequency arrays, lowerCount[26] and upperCount[26], to store how many times each lowercase or uppercase letter appears.
  • Next, we loop through the string and update the count in the respective array based on whether the character is uppercase or lowercase.
  • Then, we build the result using two pointers (i for uppercase and j for lowercase). We also use a third pointer, k, to place characters back into the original string.
  • We start with the smallest available uppercase letter, then take the smallest lowercase letter, and repeat this process until the string is filled.

C++ Program for Alternate Lower Upper Case Letters

Following is the C++ program where we sort and rearrange the given string alternately with uppercase and lowercase letters.

#include <iostream>
using namespace std;
#define MAX 26

// Function to sort and place lowercase and uppercase letters alternately
void alternateULSort(string& s) {
   int n = s.length();
   int lowerCount[MAX] = {0}, upperCount[MAX] = {0};

   // Count frequency of each uppercase and lowercase letter
   for (int i = 0; i < n; i++) {
      if (isupper(s[i]))
         upperCount[s[i] - 'A']++;
      else
         lowerCount[s[i] - 'a']++;
   }
   int i = 0, j = 0, k = 0;

   // Alternate placement of uppercase and lowercase letters
   while (k < n) {
      while (i < MAX && upperCount[i] == 0)
         i++;
      if (i < MAX) {
         s[k++] = 'A' + i;
         upperCount[i]--;
      }

      while (j < MAX && lowerCount[j] == 0)
         j++;
      if (j < MAX) {
         s[k++] = 'a' + j;
         lowerCount[j]--;
      }
   }
}

int main() {
   string str = "TutoRiALsPoInT";
   cout << "The unsorted string is: " << str;
   cout << "\nThe alternate lower upper sorted string is: ";
   alternateULSort(str);  // Call the function to sort alternately
   cout << str << "\n";
}

Following is the output of the above program:

The unsorted string is: TutoRiALsPoInT
The alternate lower upper sorted string is: AiInLoPoRsTtTu

Time Complexity: O(n), where n is the length of the string.

Space Complexity: O(1) because we use fixed-size arrays of size 26.

Conclusion

In this article, we learned how to arrange uppercase and lowercase letters in an alternate sorted order using frequency arrays. This solution is simple and works well with a time complexity of O(n) and a space complexity of O(1).

Updated on: 2025-07-29T14:28:24+05:30

587 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements