标签:vol count har sage += auto begin col cond
bool compare(pair<int,char>a, pair<int,char>b) { return a.first>b.first; } class Solution { public: string reorganizeString(string S) { auto length=S.size(); if (length<=1) return S; int counts[26]={0}; for (int i=0;i<length;i++) ++counts[S[i]-‘a‘]; int threshold=length/2+(length%2==0?0:1); for (int i=0;i<26;i++) { if (counts[i]>threshold) return ""; } vector<pair<int,char>> characters_involved; for (int i=0;i<26;i++) { if (counts[i]>0) { characters_involved.push_back(make_pair(counts[i],‘a‘+i)); } } sort(characters_involved.begin(), characters_involved.end(),compare); //sort characters_involved in decreasing order based on the counts. string answer(length, ‘ ‘); auto current_choice=characters_involved.begin(); int current_position=0; while (current_choice!=characters_involved.end()) { answer[current_position]=current_choice->second; if (--(current_choice->first)==0) ++current_choice; current_position+=2; if (current_position>=length) current_position=1; } return answer; } };
标签:vol count har sage += auto begin col cond
原文地址:https://www.cnblogs.com/RDaneelOlivaw/p/10353955.html