Java, Priority Queue and stack -  Larger String with repeat limit
At first on looking into this problem it looked little complex,\n\nBut after thinking for some time, I am able to unwire it little easily..\n\nHere is the appro
At first on looking into this problem it looked little complex,\n\nBut after thinking for some time, I am able to unwire it little easily..\n\nHere is the approach I took.\n\nIn-order to get a lexicographically larger number we know that it should have all larger alphabets in the starting of string like zzzzzzyyyyyyxxxxx.\n\nBut here they have given repeat count limit, so first things first we can sort the given string of characters using PriorityQueue.\n\n`1. Sort the given string in lexicographically large order using Max heap\n\nThen to maintain the repeat count and the lesser lexicographically small character once and then come back to larger charecter, I took the help of stack.\n\n 2. If the count of repeated character is more than the given repeat limit then add to stack\n 3. Then add the small character once and check whether the stack is empty\n 4. If its empty then you can continue in the same pattern\n 5. If stack is not empty then add the characters to its repeat count and again go for lesser character and repeat this process till the Priority Queue is empty.\n\nNow its time to look into the code :)\n\n\n```\n   public String repeatLimitedString(String s, int repeatLimit) {\n        PriorityQueue<Character> pq = new PriorityQueue<Character>((a,b)->b-a);\n        for(char ch:s.toCharArray()){\n        \tpq.add(ch);\n        }\n        StringBuffer res = new StringBuffer();\n        ArrayList<Character> list = new ArrayList<Character>();\n        Stack<Character> stk = new Stack<Character>();\n        int count = 0;\n        char previouschar = pq.peek();\n        while(!pq.isEmpty()){\n        \tchar curr = pq.poll();\n        \tif(curr==previouschar){\n        \t\tif(count<repeatLimit){\n        \t\t\tres.append(curr);\n        \t\t}\n        \t\telse{\n        \t\t\tstk.add(curr);\n        \t\t}\n        \t\tcount++;\n        \t}\n        \telse{\n        \t\tif(stk.isEmpty()){\n        \t\t\tcount=0;\n        \t\t\tres.append(curr);\n        \t\t\tpreviouschar = curr;\n        \t\t\tcount++;\n        \t\t}\n        \t\telse{\n        \t\t\tres.append(curr);\n        \t\t\tcount=0;\n        \t\t\twhile(!stk.isEmpty() && count<repeatLimit){\n        \t\t\t\tres.append(stk.pop());\n        \t\t\t\tcount++;\n        \t\t\t}\n        \t\t}\n        \t}\n        }\n        return res.toString();\n    }\n\n\nHappy learning.. upvote if its helpful\n\n
['Stack', 'Heap (Priority Queue)', 'Java']