Search code examples
arraysstringsortingc++11priority-queue

Alphabetically sorting upper followed by its respective lower and everything should be in sorted manner


There is no duplicates present in the input string .

Ex AbBa output = AaBb

Ex = ACdac output = AaCcd

Ex = ADaEedb output = AabDdEe

please guide me my code not run for the last test case anyone having diffrent idea please comment if lang will be c++ than it will be easy for me to understand

c++

#include<bits/stdc++.h>
using namespace std;

void swap(char *a,char *b){
char temp = *a;
*a = *b;
*b =temp;
}
int main(){
string copy1;
string s ;
cin>>s;
int j=-1;

int left = 0;
int right = s.length()-1;

while(left<right){

    if(s[left] >= 'a'  && s[right]<='z'){
        swap(&s[left],&s[right]);
        right--;
    }
    else
        left++;

}

cout<<s<<endl;
priority_queue <char, vector<char>, greater<char> > pq;
    for(int i=0;i<s.length();i++){
            if(s[i]>='A' && s[i]<='Z'){
                pq.push(s[i]);
                }
    }

    for(int i=0;i<s.length();i++){
            if(pq.empty()==false){
                char m = pq.top();
                if(find(s.begin(),s.end(),(char)(m+32))!=s.end()){
                        copy1+=(char)m;
                        copy1+=(char)(m+32);
                        pq.pop();
                    }
                else{
                copy1+=(char)m;
                pq.pop();
                }
            }
    }

cout<<copy1<<endl;

}
*/

Solution

  • A quick implementation using as much of the standard library as possible:

    #include <cctype>
    #include <algorithm>
    #include <string>
    #include <iostream>
    
    int main(){
        auto compare = [](char a, char b){
            char upperA = std::toupper(a);
            char upperB = std::toupper(b);
            if(upperA != upperB) return upperA < upperB;
            if(upperA == a) return true;
            return false;
        };
    
        std::string input = "ADaEedbaaaaa";
    
        std::sort(input.begin(), input.end(), compare);
        auto endOfUniques = std::unique(input.begin(), input.end());
        input.erase(endOfUniques, input.end());
        std::cout << input << std::endl;
    }
    

    If you absolutely have to use std::priority_queue rather than std::sort, this slightly less elegant version works, although it doesn't do any de-duplication (it's not clear to me if "NO Duplicates allowed in string" is a requirement for the input or the output):

    #include <cctype>
    #include <string>
    #include <iostream>
    #include <queue>
    
    int main(){
        struct compare{
            bool operator()(char a, char b){
                char upperA = std::toupper(a);
                char upperB = std::toupper(b);
                if(upperA != upperB) return upperA > upperB;
                if(upperA == a) return false;
                return true;
            }
        };
    
        std::string input = "ADaEedb";
    
        std::priority_queue<char, std::vector<char>, compare> queue;
        for(auto c : input) queue.push(c);
    
        std::string output;
        while(!queue.empty()){
            output += queue.top();
            queue.pop();
        }
    
        std::cout << output << std::endl;
    }