Construct String With Repeat Limit | Leetcode 2182

Construct String With Repeat Limit | Leetcode 2182

Techdose

54 года назад

3,205 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@kuriilo
@kuriilo - 17.12.2024 03:15

Nice one!

Ответить
@jatinpabari
@jatinpabari - 17.12.2024 06:34

Thanks for the detailed explanation

Ответить
@tmkocvines7348
@tmkocvines7348 - 17.12.2024 09:25

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;


}
};

Ответить
@Abhishek-e6d2c
@Abhishek-e6d2c - 17.12.2024 09:45

loved it!!

Ответить
@ujjwal_7531
@ujjwal_7531 - 17.12.2024 16:00

my code was very close but was giving rte, now its cool

Ответить
@AgastyaXV
@AgastyaXV - 17.12.2024 20:58

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!

Ответить
@shekhar9595
@shekhar9595 - 18.12.2024 13:24

sir aaj ke question ki video nhi aayi

Ответить