Комментарии:
Nice one!
ОтветитьThanks for the detailed explanation
Ответитьi was able to slve it by myself
class Solution {
public:
string repeatLimitedString(string s, int l) {
auto cmp = [](pair<int, char> a, pair<int, char> b) {
return a.second < b.second; // Compare second element (char)
};
unordered_map<char,int>m;
// Priority queue with custom comparator
priority_queue<pair<int, char>, vector<pair<int, char>>, decltype(cmp)> p(cmp);
string ans;
for(int i=0;i<s.size();i++)
{
m[s[i]]++;
}
for(auto i:m)
{
p.push({i.second,i.first});
}
while(!p.empty())
{
int a=p.top().first;
char c=p.top().second;
p.pop();
int r=l;
if(a>r)
{
string str(r, c);
ans+=str;
if(p.empty()==true)
{
break;
}
else
{
int a1=p.top().first;
char c1=p.top().second;
p.pop();
ans+=c1;
p.push({a-r,c});
if(a1-1>0)
p.push({a1-1,c1});
}
}
else
{
string str(a, c);
ans+=str;
}
}
return ans;
}
};
loved it!!
Ответитьmy code was very close but was giving rte, now its cool
ОтветитьHey bhaiya,
I just had a few questions and wanted to know where could i contact you
To summarise it up, i just wanted to know that how many hours and how many months would it take to properly learn dsa and development etc. This is coming from a 3rd sem student currently doing his bachelors
Thank you!
sir aaj ke question ki video nhi aayi
Ответить