Given a non-empty string str and an integer K, rearrange the string such, the same characters is at least distance K from each of the other.
All input strings is given in lowercase letters. If It is not a possible to rearrange the string, return an empty string ""
.
Example 1:
str = "AABBCC", K = 3Result: "ABCABC" the same letters is at least distance 3 from all other.
Example 2:
str = "Aaabc", K = 3 Answer: "It is not possible to rearrange the string.
Example 3:
str = "AAADBBCC", K = 2Answer: "ABACABCD" Another possible answer is: "ABCABCDA" the same letters was at least distance 2 fr Om each of the other.
Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.
analysis:solution 1:everytime, we add the char X with the largest count, after then we should put it back to the Que UE (named Readyq) to find out the next largest char. However, does not forget the constraint of K apart. So we should make char X waiting for K-1 arounds of fetching and then put the it back to queue. We use another queue (named Waitingq) to store the waiting chars. Whenever, the size of this queue equals to K, the head C Har is a ready-to-go back to READYQ. solution 2:based on Solution 1, we don't use the priorityqueue, instead, we just use array with + elements to store CH Ars ' Count and its next available position. Every round, we iterate through the arrays and find out of the available char with Max Count. note:theoretically, the C Omplexity of Solution 1 using Priorityqueue is O (Nlog (a)), while the complexity of Solution 2 are O (n*26) which is larger than solution 1. However, in real implementation, because solution 1 involves creating more complex data structuresand sorting them, Soluiont 1 is much slower than solution 2. solution 1:
ImportJava.util.Map.Entry; Public classSolution { PublicString rearrangestring (String str,intk) {if(Str.isempty () | | k<0)return""; if(k==0)returnstr; HashMap<Character,Integer> Charmap =NewHashmap<character,integer>(); for(CharC:str.tochararray ()) {Charmap.put (C,charmap.getordefault (c,0) +1); } Char[] chars =New Char[Charmap.size ()]; int[] CharCount =New int[Charmap.size ()]; int[] valid =New int[Charmap.size ()]; intindex = 0; for(Entry entry:charMap.entrySet ()) {Charc = (Char) Entry.getkey (); intCount = (int) Entry.getvalue (); Chars[index]=C; Charcount[index++] =count; } StringBuilder Builder=NewStringBuilder (); intNextind = 0; intNextchar =GetNextChar (Charcount,valid,nextind); while(Nextchar!=-1) {builder.append (Chars[nextchar]); Charcount[nextchar]--; Valid[nextchar]= (nextind++) +K; Nextchar=GetNextChar (Charcount,valid,nextind); } if(Builder.length ()!=str.length ())return""; returnbuilder.tostring (); } Public intGetNextChar (int[] charcount,int[] Valid,intnextind) { intMaxCount = 0; intindex =-1; for(inti=0;i<charcount.length;i++) if(Valid[i]<=nextind && charcount[i]>MaxCount) {MaxCount=Charcount[i]; Index=i; } returnindex; }}
Solution 2:
ImportJava.util.Map.Entry; Public classSolution { PublicString rearrangestring (String str,intk) {if(Str.isempty () | | k<0)return""; if(k==0)returnstr; int[] CharCount =New int[26]; int[] valid =New int[26]; Arrays.fill (Valid,-1); for(CharC:str.tochararray ()) {Charcount[c-' A ']++; } for(inti=0;i<26;i++) if(charcount[i]>0) {Valid[i]= 0; } StringBuilder Builder=NewStringBuilder (); intNextind = 0; intNextchar =GetNextChar (Charcount,valid,nextind); while(Nextchar!=-1) {builder.append (Char) (nextchar+ ' a ')); Charcount[nextchar]--; Valid[nextchar]= (nextind++) +K; Nextchar=GetNextChar (Charcount,valid,nextind); } if(Builder.length ()!=str.length ())return""; returnbuilder.tostring (); } Public intGetNextChar (int[] charcount,int[] Valid,intnextind) { intMaxCount = 0; intindex =-1; for(inti=0;i<charcount.length;i++) if(Valid[i]<=nextind && charcount[i]>MaxCount) {MaxCount=Charcount[i]; Index=i; } returnindex; }}
Leetcode-rearrange String k Distance Apart