id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<pair<int,int>>v;\n\n for(auto it : meetings){\n v.push_back({it[0],it[1]});\n }\n\n sort(v.begin(),v.end());\n\n // for(auto it :v){\n // cout<<it.first<<\" \"<<it.second<<endl;\n // }\n\n vector<pair<int,int>>arr;\n\n arr.push_back(v[0]);\n\n for(int i=1;i<v.size();i++){\n if(arr.back().second>=v[i].first){\n arr.back()={arr.back().first,max(v[i].second,arr.back().second)};\n }\n else{\n arr.push_back(v[i]);\n }\n }\n\n // cout<<endl;\n\n // for(auto it :arr){\n // cout<<it.first<<\" \"<<it.second<<endl;\n // }\n\n int count=0;\n\n for(auto it : arr){\n count+=it.second-it.first+1;\n }\n\n return days-count;\n }\n};", "memory": "150400" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<pair<int,int>>v;\n\n for(auto it : meetings){\n v.push_back({it[0],it[1]});\n }\n\n sort(v.begin(),v.end());\n\n for(auto it :v){\n cout<<it.first<<\" \"<<it.second<<endl;\n }\n\n vector<pair<int,int>>arr;\n\n arr.push_back(v[0]);\n\n for(int i=1;i<v.size();i++){\n if(arr.back().second>=v[i].first){\n arr.back()={arr.back().first,max(v[i].second,arr.back().second)};\n }\n else{\n arr.push_back(v[i]);\n }\n }\n\n cout<<endl;\n\n for(auto it :arr){\n cout<<it.first<<\" \"<<it.second<<endl;\n }\n\n int count=0;\n\n for(auto it : arr){\n count+=it.second-it.first+1;\n }\n\n return days-count;\n }\n};", "memory": "150500" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<int> start;\n vector<int> end;\n int n = meetings.size();\n for(auto it: meetings){\n start.push_back(it[0]);\n end.push_back(it[1]);\n }\n\n sort(start.begin(), start.end());\n sort(end.begin(), end.end());\n\n int cnt = 0;\n if(start[0] > 1){\n cnt += start[0]-1;\n }\n int i = 1, j = 0;\n int meet = 1;\n while(i < n && j < n){\n if(start[i] <= end[j]){\n meet++;\n i++;\n } else {\n meet--;\n if(meet == 0){\n cnt += start[i]-end[j]-1;\n }\n j++;\n }\n }\n if(end[n-1] < days){\n cnt+= days-end[n-1];\n }\n return cnt;\n }\n};", "memory": "150700" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<vector<int>>v=meetings;\n int n=v.size();\n int ans=0;\n sort(v.begin(),v.end());\n int mx=v[0][1];\n for(int i=1;i<n;i++)\n {\n if(v[i][0]>mx+1)\n {\n ans+=(v[i][0]-mx-1);\n mx=v[i][1];\n }\n \n else\n mx=max(mx,v[i][1]);\n }\n if(days>mx)ans+=days-mx;\n if(1<v[0][0])ans+=v[0][0]-1;\n\n return ans;\n }\n};", "memory": "151800" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<vector<int>>v=meetings;\n int n=v.size();\n int ans=0;\n sort(v.begin(),v.end());\n int mx=v[0][1];\n for(int i=1;i<n;i++)\n {\n if(v[i][0]>mx+1)\n {\n ans+=(v[i][0]-mx-1);\n mx=v[i][1];\n }\n \n else\n mx=max(mx,v[i][1]);\n }\n if(days>mx)ans+=days-mx;\n if(1<v[0][0])ans+=v[0][0]-1;\n\n return ans;\n }\n};", "memory": "151900" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<vector<int>> v = meetings;\n sort(v.begin(),v.end());\n int cnt = 0 ;\n int n = meetings.size();\n int endT = v[0][1];\n cnt += v[0][1] - v[0][0] + 1;\n\n for(int i = 1 ; i < n ;i++){\n if(endT >= v[i][1]) continue;\n else if(v[i][0] <= endT){\n cnt += v[i][1] - endT;\n }\n else{\n cnt += v[i][1] - v[i][0] + 1;\n }\n endT = max(endT,v[i][1]);\n }\n\n return days - cnt;\n }\n};", "memory": "152000" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> transform(vector<vector<int>> m){\n int l=m[0][0],r=m[0][1];\n vector<vector<int>> res;\n\n for(int i=1;i<m.size();i++){\n if(m[i][0]<=r) r=max(r,m[i][1]);\n else{\n res.push_back({l,r});\n l=m[i][0],r=m[i][1];\n }\n }\n\n res.push_back({l,r});\n\n return res;\n }\n\n int countDays(int days, vector<vector<int>>& m) {\n int prev=1,ans=0;\n sort(m.begin(),m.end());\n\n m=transform(m);\n\n for(int i=0;i<m.size();i++){\n ans+=(m[i][0]-prev);\n prev=m[i][1]+1;\n }\n\n ans+=(days-prev+1);\n\n return ans;\n }\n};", "memory": "152200" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\n vector<vector<int>> overlap(vector<vector<int>> meetings)\n {\n vector<vector<int>> merged;\n sort(meetings.begin(), meetings.end());\n merged.push_back(meetings[0]);\n\n for(auto& interval : meetings)\n {\n if(interval[0] <= merged.back()[1])\n {\n merged.back()[1] = max(merged.back()[1], interval[1]);\n }\n else\n {\n merged.push_back(interval);\n }\n }\n return merged;\n }\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n vector<vector<int>> merged = overlap(meetings);\n int count = 0;\n for(auto interval : merged)\n {\n int diff = interval[1] - interval[0] + 1;\n count += diff;\n }\n return days - count;\n }\n};", "memory": "152400" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n multimap<int, int> mpp;\n for(int i = 0; i < meetings.size(); i++){\n mpp.insert({meetings[i][0], meetings[i][1]});\n }\n \n int ans = 0;\n int maxi = 0;\n \n for (auto it = mpp.begin(); it != mpp.end(); it++) {\n // If there's a gap between the current maximum end time and the start of the next meeting\n if (it->first > maxi + 1) {\n ans += it->first - maxi - 1;\n }\n // Update the maximum end time considering overlapping meetings\n maxi = max(maxi, it->second);\n }\n \n // Calculate the gap after the last meeting\n ans += days - maxi;\n \n return ans;\n }\n};\n", "memory": "155100" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nclass Solution {\npublic:\n int countDays(int d, vector<vector<int>>& v) {\n sort(all(v));\n ll n=v.size();\n set<ii> st;\n st.insert({v[0][0],v[0][1]});\n REP(i,1,n-1){\n ii prv=*st.rbegin();\n if(prv.S<v[i][0])st.insert({v[i][0],v[i][1]});\n else{\n st.erase(prv);\n ll mn=min(prv.F,(ll)v[i][0]);\n ll mx=max(prv.S,(ll)v[i][1]);\n st.insert({mn,mx});\n }\n }\n for(auto i:st){\n d-=(i.S-i.F+1);\n }\n return d;\n }\n};", "memory": "155200" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n priority_queue<pair<int,char>, vector<pair<int,char>>, greater<pair<int,char>>> pq;\n for(auto m:meetings){\n pq.push({m[0],'A'});\n pq.push({m[1],'D'});\n }\n int arr = 0;\n int arrtime = 0;\n int ndays = 0;\n while(!pq.empty()){\n auto f = pq.top();\n pq.pop();\n int d = f.first;\n char c = f.second;\n if(c=='A'){\n if(arr==0){\n arr++;\n arrtime = d;\n }\n else{\n arr++;\n }\n }\n if(c=='D'){\n if(arr==1){\n arr--;\n ndays+=d-arrtime+1;\n }\n if(arr>1){\n arr--;\n }\n }\n }\n return days-ndays;\n }\n};", "memory": "158000" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "/*\n\nhow many days a person is available for work\nmeetings array\n\ndays is 10^9 so you cant overlap\n\nbasically you need to know when he is free\n\n2 solutions come to my mind\neither sort and merge intervals\n\nor push events in priority queue\nthis sounds easier \n\nlets attempt\n\n4 4 \n\n4 6\n\n*/\n\n#define pii pair<int, int>\n\nenum EventType {END = 1, START = -1};\n\nclass Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n priority_queue<pii, vector<pii>, greater<pii>> events;\n for (auto meeting : meetings) {\n events.push({meeting[0], START});\n events.push({meeting[1], END});\n } \n int last_occupied_day = 0;\n int meeting_count = 0;\n int free_days = 0;\n while (!events.empty()) {\n pii event = events.top(); events.pop();\n int meeting_day = event.first;\n int event_type = event.second;\n meeting_count -= event_type;\n if (meeting_count == 1 && event_type == START) {\n free_days += max(meeting_day - last_occupied_day - 1, 0);\n } else if (meeting_count == 0 && event_type == END) {\n last_occupied_day = meeting_day;\n }\n }\n cout << free_days << ' ' << last_occupied_day << endl;\n free_days += days - last_occupied_day;\n return free_days;\n }\n};", "memory": "158100" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // merge intervals\n // sort by start time\n // [1,2], [2,3], [4,5]\n int countDays(int days, vector<vector<int>>& meetings) {\n std::sort(meetings.begin(), meetings.end());\n vector<int> prev = meetings[0];\n int res = prev[0]-1;\n\n for (int i=1; i<meetings.size(); ++i) {\n std::vector<int> meeting = meetings[i];\n if (is_overlap(prev, meeting)) {\n prev = merge(prev, meeting);\n } else {\n // add diff between prev and current meeting\n res += meeting[0]-prev[1]-1;\n prev = meeting;\n }\n }\n res += days-prev[1];\n return res;\n }\n\n bool is_overlap(vector<int>& first, vector<int>& second) {\n // assume first[0] <= second[0]\n return second[0] <= first[1];\n }\n\n std::vector<int> merge(vector<int>& first, vector<int>& second) {\n // assumes both are overlapping\n return {std::min(first[0], second[0]), std::max(first[1], second[1])};\n }\n};", "memory": "158200" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n int n = meetings.size();\n sort(meetings.begin(), meetings.end());\n vector<vector<int>> ans;\n ans.push_back(meetings[0]);\n for(int i = 1; i < n; i++) {\n vector<int> v = ans.back();\n if(meetings[i][0] <= v[1]) {\n ans.pop_back();\n ans.push_back({v[0], max(meetings[i][1], v[1])});\n }\n else {\n ans.push_back(meetings[i]);\n }\n }\n // for(vector<int> v : ans) {\n // cout << v[0] << \" \" << v[1] << endl;\n // }\n int c = ans[0][0] - 1;\n for(int i = 1; i < ans.size(); i++) {\n c += (ans[i][0] - ans[i - 1][1] - 1);\n }\n c += days - ans.back()[1];\n return c;\n }\n};", "memory": "158300" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n \n sort(meetings.begin(),meetings.end(),[](const vector<int>& a,const vector<int>& b){\n return a[0]<b[0];\n });\n\n vector<vector<int>> meet;\n meet.push_back(meetings[0]);\n auto last = meet[0];\n\n for(int i=1;i<meetings.size();i++)\n {\n auto curr = meetings[i];\n\n if(curr[0]>last[1]+1)\n {\n meet.push_back(curr);\n last = curr;\n }\n else\n {\n last[0]= min(last[0],curr[0]);\n last[1] = max(last[1],curr[1]);\n\n meet.pop_back();\n meet.push_back(last);\n }\n }\n\n int d = 0;\n int ans = 0;\n\n for(int i=0;i<meet.size();i++)\n {\n ans += (meet[i][0]-d-1);\n d = meet[i][1];\n }\n\n ans += days - d;\n return ans;\n }\n};", "memory": "158400" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n vector<vector<int>>merge_meetings;\n merge_meetings.push_back(meetings[0]);\n int n = meetings.size();\n for(int i = 1; i < n; ++i){\n auto it = merge_meetings[merge_meetings.size()-1];\n int s = meetings[i][0];\n int e = meetings[i][1];\n int e1 = it[1];\n int s1 = it[0];\n if(s<=e1){\n merge_meetings.pop_back();\n merge_meetings.push_back({s1, max(e1, e)});\n }\n else{\n merge_meetings.push_back({s, e});\n } \n }\n int m = 0;\n for(auto it:merge_meetings){\n int s = it[0];\n int e = it[1];\n //cout << \"s e \" << s << \" \" << e << endl;\n m+=(e-s+1);\n }\n //cout << merge_meetings.size() << endl;\n if(days>=m)\n return days-m;\n else\n return 0;\n }\n};", "memory": "158500" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // merge intervals\n // sort by start time\n // [1,2], [2,3], [4,5]\n int countDays(int days, vector<vector<int>>& meetings) {\n std::sort(meetings.begin(), meetings.end());\n vector<int> prev = meetings[0];\n vector<vector<int>> non_overlap;\n\n for (int i=1; i<meetings.size(); ++i) {\n std::vector<int> meeting = meetings[i];\n if (is_overlap(prev, meeting)) {\n prev = merge(prev, meeting);\n } else {\n non_overlap.push_back(prev);\n prev = meeting;\n }\n }\n non_overlap.push_back(prev);\n\n int res = days;\n for (auto& meeting : non_overlap) {\n res -= meeting[1]-meeting[0]+1;\n }\n return res;\n }\n\n bool is_overlap(vector<int>& first, vector<int>& second) {\n // assume first[0] <= second[0]\n return second[0] <= first[1];\n }\n\n std::vector<int> merge(vector<int>& first, vector<int>& second) {\n // assumes both are overlapping\n return {std::min(first[0], second[0]), std::max(first[1], second[1])};\n }\n};", "memory": "158600" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n stack<vector<int>> withoutOverlaps;\n sort(meetings.begin(), meetings.end());\n for (const auto& item : meetings) {\n if (withoutOverlaps.empty()) {\n withoutOverlaps.push(item);\n }\n else {\n const auto top = withoutOverlaps.top();\n // overlap\n if (item[0] <= top[1]) {\n withoutOverlaps.pop();\n withoutOverlaps.push({ top[0], max(item[1], top[1]) });\n }\n else {\n withoutOverlaps.push(item);\n }\n }\n }\n \n int count = 0;\n if (!withoutOverlaps.empty()) {\n auto top = withoutOverlaps.top();\n withoutOverlaps.pop();\n \n count += days - top[1];\n while (!withoutOverlaps.empty()) {\n auto newTop = withoutOverlaps.top();\n withoutOverlaps.pop();\n count += top[0] - newTop[1] - 1;\n top = newTop;\n }\n count += top[0] - 1;\n }\n return count;\n }\n};", "memory": "159200" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n stack<vector<int>> withoutOverlaps;\n sort(meetings.begin(), meetings.end());\n for (const auto& item : meetings) {\n if (withoutOverlaps.empty()) {\n withoutOverlaps.push(item);\n }\n else {\n const auto top = withoutOverlaps.top();\n // overlap\n if (item[0] <= top[1]) {\n withoutOverlaps.pop();\n withoutOverlaps.push({ top[0], max(item[1], top[1]) });\n }\n else {\n withoutOverlaps.push(item);\n }\n }\n }\n\n int count = 0;\n if (!withoutOverlaps.empty()) {\n auto top = withoutOverlaps.top();\n withoutOverlaps.pop();\n \n count += days - top[1];\n while (!withoutOverlaps.empty()) {\n auto newTop = withoutOverlaps.top();\n withoutOverlaps.pop();\n count += top[0] - newTop[1] - 1;\n top = newTop;\n }\n count += top[0] - 1;\n }\n\n //int count = 0;\n //int i = days;\n //while (i >= 1) {\n // if (!withoutOverlaps.empty()) {\n // const auto& top = withoutOverlaps.top();\n // if (i >= top[0] && i <= top[1]) {\n // --i;\n // continue;\n // }\n // else {\n // if (i < top[0]) {\n // withoutOverlaps.pop();\n // }\n // else {\n // ++count;\n // --i;\n // }\n // }\n // }\n // else {\n // ++count;\n // --i;\n // }\n //}\n\n return count;\n }\n};", "memory": "159400" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) \n {\n sort(meetings.begin(), meetings.end());\n stack<vector<int>> s;\n for(vector<int>& interval:meetings)\n {\n if(s.empty())\n {\n s.push(interval);\n }\n else\n {\n vector<int> topinterval=s.top();\n if(interval[0]>topinterval[1])\n {\n s.push(interval);\n }\n else\n {\n topinterval[1]=max(topinterval[1], interval[1]);\n s.pop();\n s.push(topinterval);\n }\n }\n }\n vector<vector<int>> temp, finalans;\n while(!s.empty())\n {\n temp.push_back(s.top());\n s.pop();\n }\n for(int i=temp.size()-1;i>=0;i--)\n {\n finalans.push_back(temp[i]);\n }\n int count=0;\n for(int i=0;i<finalans.size()-1;i++)\n {\n count += finalans[i+1][0]-finalans[i][1]-1;\n }\n count += days-finalans[finalans.size()-1][1];\n if(finalans[0][0]>1)\n {\n count += finalans[0][0]-1;\n }\n return count;\n }\n};", "memory": "159500" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n map<int,int> s;\n for (auto &x: meetings) {\n int u = x[0], v = x[1];\n s[u]++, s[v]--;\n }\n\n int ans = 0, l = 0, cur = 0;\n for (auto [d, v]: s) {\n if (!cur && d>l) {\n //cout << l << \" \" << d << endl;\n ans += d-l-1;\n }\n cur += v;\n l = d;\n }\n\n ans += days-l;\n\n return ans;\n }\n};", "memory": "159600" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n map<int, int> line;\n \n for (auto& meeting : meetings) {\n int st = meeting[0], en = meeting[1];\n line[st] += 1;\n line[en+1] -= 1;\n }\n\n int prevCount = 0, ans = 0, currDay = 1;\n \n for (auto& [day, cnt] : line) {\n cnt += prevCount;\n if (prevCount == 0) ans += (day - currDay);\n prevCount = cnt;\n currDay = day;\n }\n ans += (days - currDay + 1);\n \n return ans;\n }\n};", "memory": "159700" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& schedule) {\n map<int, int> meetings;\n for (auto& meet : schedule) {\n meetings[meet[0]] += 1;\n meetings[meet[1] + 1] -= 1;\n }\n int meet_count=0;\n for(auto it=meetings.begin();it!=meetings.end();++it){\n it->second+=meet_count;\n meet_count = it->second;\n }\n int free_days = 0;\n auto itr = meetings.begin();\n if(itr->first>1)\n free_days+=itr->first-1;\n for(auto it=meetings.begin();it!=meetings.end();++it){\n if(it->second==0){\n if(next(it,1)==meetings.end()){\n if(it->first>days)\n continue;\n else\n free_days+=days-it->first+1;\n }else{\n free_days+=next(it,1)->first-it->first;\n }\n }\n }\n return free_days;\n }\n};", "memory": "159800" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n map<int,int> md;\n for(vector<int> &m:meetings)\n {\n md[m[0]]+=1;\n md[m[1]+1]-=1;\n }\n int ans=0,lasti=1,cnt=0;\n for(auto [idx,diff]:md)\n {\n if(cnt==0) ans+=idx-lasti;\n cnt+=diff;\n lasti=idx;\n }\n if(cnt==0 && lasti<=days) ans+=days+1-lasti;\n return ans;\n }\n};", "memory": "159900" }
3,430
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p> <p>Return the count of days when the employee is available for work but no meetings are scheduled.</p> <p><strong>Note: </strong>The meetings may overlap.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is no meeting scheduled on the 5<sup>th </sup>day.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>Meetings are scheduled for all working days.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li> <li><code>meetings[i].length == 2</code></li> <li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countDays(int days, vector<vector<int>>& meetings) {\n map<int, int> mp;\n int n = meetings.size();\n for(int i=0; i<n; i++) {\n mp[meetings[i][0]] += 1;\n mp[meetings[i][1]+1] -= 1;\n }\n \n vector<int> prefix(mp.size() + 1);\n int i = 1;\n for(auto& [day, count] : mp) {\n prefix[i] = prefix[i-1] + count;\n i++;\n }\n\n int ans = mp.begin()->first - 1;\n i = 1;\n for(auto it = next(mp.begin()); it != mp.end(); ++it, ++i) {\n if(prefix[i] <= 0) {\n ans += it->first - prev(it)->first;\n }\n }\n\n if(prefix.back() == 0) {\n ans += max(0, days - mp.rbegin()->first + 1);\n }\n \n return ans;\n }\n};\n", "memory": "162100" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string clearStars(string &s)\n {\n vector<vector<int>> pos(26,vector<int>(0));\n for(int i=0;i<s.length();i++)\n {\n if(s[i]!='*')\n pos[s[i]-'a'].push_back(i);\n \n \n if(s[i]=='*')\n {\n for(int i=0;i<26;i++)\n {\n if(pos[i].size()>0)\n {\n s[pos[i].back()]='@';\n pos[i].pop_back();\n // pos[i]=-1;\n break;\n }\n \n}\n \n}\n}\n string ans=\"\";\n for(auto a:s)\n {\n if(a!='@'&&a!='*')\n ans.push_back(a);\n }\n return ans;\n }\n};", "memory": "21659" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n if (s.find(\"*\") == string::npos)\n return s;\n\n string ans;\n vector<vector<int>> bucket(26, vector<int>(0));\n\n for (size_t charIndex = 0; charIndex < s.length(); ++charIndex) {\n if (s[charIndex] == '*') {\n for (size_t i = 0; i < 26; ++i) {\n if (not bucket[i].empty()) {\n int lastIndex = bucket[i].back();\n\n s[lastIndex] = '0';\n bucket[i].pop_back();\n\n break;\n }\n }\n }\n else {\n int idx = s[charIndex] - 'a';\n bucket[idx].push_back(charIndex);\n }\n }\n\n for (const char ch : s)\n if (ch == '*' or ch == '0')\n continue;\n else\n ans += ch;\n\n return ans;\n }\n};", "memory": "21659" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size(), m = 0;\n vector<int> idx[26];\n for (int i = 0, j = 0; j < n; ++j) {\n if (s[j] == '*') {\n ++m;\n for (int i = 0; i < 26; ++i)\n if (idx[i].empty()) continue;\n else {\n s[idx[i].back()] = '*';\n idx[i].pop_back();\n break;\n }\n }\n else {\n idx[s[j]-'a'].push_back(j);\n }\n }\n \n for (int i = 0, j = 0; j < n; ++j) {\n if (s[j] == '*') continue;\n else s[i++] = s[j];\n }\n return s.substr(0, n-m*2);\n }\n};", "memory": "23778" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n vector<vector<int>>dp(26);\n int n = s.size();\n int i;\n for(i=0;i<n;i++){\n if(s[i]!='*'){\n dp[s[i]-'a'].push_back(i);\n }\n else{\n for(int j=0; j<26; j++){\n int m = dp[j].size();\n if(m){\n s[dp[j][m-1]] = '#';\n dp[j].pop_back();\n break;\n }\n }\n }\n }\n string res=\"\";\n for(i=0;i<n;i++){\n if(s[i]>='a' && s[i]<='z'){\n res+=s[i];\n }\n }\n return res;\n }\n};", "memory": "23778" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n typedef pair<int, int> pii;\n struct cmp {\n bool operator() (const pii& p1, const pii& p2) {\n return p1.first != p2.first ? p1.first > p2.first : p1.second < p2.second;\n }\n };\n string clearStars(string s) {\n priority_queue<char, vector<char>, greater<char>> pq;\n vector<vector<int>> indices(26);\n int idx = 0, n = s.length();\n char temp;\n for (int i = 0; i < n; i++) {\n if (s[i] == '*') {\n temp = pq.top();\n pq.pop();\n s[indices[temp-'a'].back()] = '*';\n indices[temp-'a'].pop_back();\n } else {\n pq.push(s[i]);\n indices[s[i]-'a'].push_back(i);\n }\n }\n string res;\n for (auto& c : s) \n if (c != '*') res.push_back(c);\n return res;\n }\n};", "memory": "25896" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n map<char,vector<int>> mp;\n\n for(int i=0;i<n;i++){\n if(s[i]!='*') mp[s[i]].push_back(i);\n else{\n for(char it = 'a'; it<='z';it++){\n if(mp[it].size()){\n s[mp[it].back()] = '0';\n // cout<<mp[it].back()<<\" \"<<it<<endl;\n mp[it].pop_back();\n break;\n }\n }\n }\n }\n string ans = \"\";\n for(int i=0;i<n;i++){\n if(s[i]!='0' && s[i]!='*') ans+=s[i];\n }\n return ans;\n\n }\n};", "memory": "25896" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n\n class IndexComparator {\n public:\n explicit IndexComparator(const string& str) : string_(str) {}\n \n bool operator()(int index1, int index2) const {\n if(string_[index1]==string_[index2]){\n return index1<index2;\n }\n return string_[index1] > string_[index2];\n }\n \n private:\n const string& string_;\n };\n\n string clearStars(string s) {\n\n // map<char , int> k ; \n\n priority_queue<int, vector<int>, IndexComparator> pq((IndexComparator(s)));\n\n int n = s.length();\n int i=0;\n\n while(i<n){\n char c= s[i];\n\n if(c=='*'){\n pq.pop();\n }\n else{\n // k[i]=c;\n pq.push(i);\n }\n\n i++;\n }\n\n vector<char> vc(n , ' ');\n\n while(!pq.empty()){\n int tp = pq.top();\n pq.pop();\n vc[tp] = s[tp];\n }\n\n string ans = \"\";\n\n for(int i=0 ; i<n ; i++){\n if(vc[i] != ' '){\n ans += vc[i];\n }\n }\n \n\n return ans;\n \n }\n};", "memory": "28015" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
1
{ "code": "class Compare {\npublic:\n bool operator()(pair<char, int>& below, pair<char, int>& above)\n {\n if (below.first > above.first) {\n return true;\n }\n else if (below.first == above.first\n && below.second < above.second) {\n return true;\n }\n\n return false;\n }\n};\nclass Solution {\npublic:\n string clearStars(string s) {\n priority_queue<pair<char, int>, vector<pair<char, int>>,Compare> pq;\n int n=s.size();\n for(int i=0;i<n;i++){\n if(s[i]!='*') pq.push({s[i], i});\n else if(pq.size()) pq.pop();\n }\n if(pq.size()==n) return s;\n vector<pair<int, char>> tmp;\n while(!pq.empty()){\n tmp.push_back({pq.top().second, pq.top().first});\n pq.pop();\n }\n sort(tmp.begin(), tmp.end());\n string ans=\"\";\n for(int i=0;i<tmp.size();i++){\n ans.push_back(tmp[i].second);\n }\n return ans;\n }\n};", "memory": "28015" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n auto cmp = [](const std::pair<char, int>& a, const std::pair<char, int>& b) {\n if (a.first == b.first)\n return a.second < b.second; // For the same character, sort by descending int\n return a.first > b.first; // Otherwise, sort by ascending character\n };\n int n = s.length();\n std::priority_queue<std::pair<char, int>, std::vector<std::pair<char, int>>, decltype(cmp)> pq(cmp);\n for(int i=0;i<n;i++){\n if(s[i] != '*'){\n pq.push({s[i], i});\n } else {\n pq.pop();\n }\n }\n vector<pair<int,char>> de;\n while(pq.size() != 0){\n de.push_back({pq.top().second, pq.top().first});\n pq.pop();\n }\n sort(de.begin(), de.end());\n string ans = \"\";\n for(int i=0;i<de.size();i++){\n ans += de[i].second;\n }\n return ans; \n }\n};", "memory": "30134" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n struct node {\n char ch;\n int idx;\n };\n \n struct compare {\n bool operator()(const node &a, const node &b) {\n if (a.ch == b.ch) return a.idx < b.idx; // max heap\n return a.ch > b.ch; // min heap\n }\n };\n \n string clearStars(string s) {\n int n = s.length();\n \n priority_queue<node, vector<node>, compare> pq;\n for (int i=0; i<n; i++) {\n char ch = s[i];\n if (ch=='*') {\n if (pq.size()>0) pq.pop();\n } else {\n pq.push({ch, i});\n }\n }\n \n vector<pair<int,char>> v;\n while (pq.size() > 0) {\n node cur = pq.top();\n pq.pop();\n \n v.push_back({cur.idx, cur.ch});\n }\n \n sort(v.begin(), v.end());\n \n string ans = \"\";\n for (pair<int, char> p : v) ans += p.second;\n \n return ans;\n }\n};", "memory": "30134" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n unordered_map<char,int>mp;\n int n = s.size();\n vector<int>v(n+1,0);\n for(int i=0; i<n; i++){\n if(s[i]=='*'){\n v[i+1] = -1;\n for(char x='a'; x<='z'; x++){\n if(mp[x]!=0){\n int ind = mp[x];\n int prev = v[ind];\n v[ind] = -1;\n mp[x] = prev;\n break;\n }\n }\n }\n else{\n v[i+1] = mp[s[i]];\n mp[s[i]] = i+1;\n }\n }\n string fin = \"\";\n for(int i=1; i<=n; i++){\n if(v[i]!=-1){\n fin += s[i-1]; \n }\n }\n return fin;\n }\n};", "memory": "32253" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n priority_queue<pair<int, int>> pq;\n string temp = \"\";\n for (int i = 0; i < s.length(); i++) {\n if (s[i] != '*') {\n pq.push({-(int)s[i], i});\n } else {\n pq.pop();\n }\n temp += ' ';\n }\n while (pq.size() > 0) {\n temp[pq.top().second] = (char)(-pq.top().first);\n pq.pop();\n }\n string ans = \"\";\n for (int i = 0; i < temp.length(); i++) {\n if (temp[i] != ' ') {\n ans += temp[i];\n }\n }\n return ans;\n }\n};", "memory": "32253" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n vector<int> mp[26];\n vector<int> include(s.size(), 1);\n for(int i = 0; i < s.size(); i++) {\n if(s[i] == '*') {\n for(int j = 0; j < 26; j++) {\n if(mp[j].size() > 0) {\n include[mp[j].back()] = 0;\n mp[j].pop_back();\n break;\n }\n }\n } else {\n mp[s[i]-'a'].push_back(i);\n }\n }\n string ans = \"\";\n for(int i = 0; i < s.size(); i++) {\n if(include[i] && s[i] != '*') {\n ans += s[i];\n }\n }\n return ans;\n }\n};", "memory": "34371" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s){\n int n = s.size();\n vector<vector<int>> buckets(26);\n vector<int> removed(n , 0);\n for(int i = 0 ; i < n ; i++){\n if(s[i] == '*'){\n removed[i] = 1;\n for(int j = 0 ; j < 26 ; j++){\n if(buckets[j].size()){\n removed[buckets[j].back()] = 1;\n buckets[j].pop_back();\n break;\n }\n }\n }\n else{\n buckets[s[i]-'a'].push_back(i);\n }\n }\n string ans;\n for(int i = 0 ; i < n ; i++){\n if(!removed[i]) ans.push_back(s[i]);\n }\n return ans;\n }\n};", "memory": "34371" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n map <char, vector <int>>m ;\n // int lowest = -1 ;\n vector <int> vis (s.size () , 0);\n for (int i = 0 ;i < s.size () ;i++){\n if (s[i]!='*'){m[s[i]].push_back(i);}\n else {\n char l = 'a';\n while (m[l].size () == 0) l ++;\n int last = m[l].back();\n m[l].pop_back();\n vis[last]=1;\n vis [i]=1;\n }\n }\n string ans = \"\";\n for (int i =0;i< s.size () ;i++){\n if (!vis[i]) ans.push_back(s[i]); \n }\n return ans;\n }\n};", "memory": "36490" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n vector<int>v[26];\n int n = s.size();\n string ans = \"\";\n vector<int>t;\n for(int i=0;i<n;i++)\n {\n if(s[i]!='*')\n {\n v[s[i]-'a'].push_back(i);\n }\n else\n {\n for(int j=0;j<26;j++)\n {\n if(v[j].size()>0)\n {\n int m = v[j].size();\n t.push_back(v[j][m-1]);\n v[j].pop_back();\n break;\n }\n }\n }\n }\n if(t.size()>0)\n {\n sort(t.begin(),t.end());\n int j=0;\n for(int i=0;i<n;i++)\n {\n if(s[i]!='*')\n {\n \n if(j<t.size() && i != t[j])\n {\n ans+=s[i];\n }\n else if(j>=t.size())\n ans+=s[i];\n else\n {\n j++;\n }\n }\n }\n }\n else\n return s;\n return ans;\n }\n};", "memory": "36490" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int k = 0;\n priority_queue<std::pair<int, int>> mh;\n for(int i = s.size()-1 ; i>=0 ; i--)\n {\n if(s[i] == '*') k++;\n else\n {\n mh.push({s[i]-'a',s.size()-1-i});\n s[i] = '#';\n if(mh.size()>k)\n {\n int index = s.size() - 1 - mh.top().second;\n s[index] = 'a' + mh.top().first;\n mh.pop();\n }\n }\n }\n for(int i = 0 ; i < s.size() ;)\n {\n if(s[i]=='*' or s[i]=='#')s.erase(i,1);\n else \n {\n i++;\n }\n }\n return s;\n }\n};\n// we have to remove the nearest smallest characters \n// if pq me push karne ke baad jo element nikala agar woh same alphabet hai then then do not push ", "memory": "42846" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.length();\n \n priority_queue<pair<char, int>> pq;\n \n vector<char> ans(n, '.');\n \n int idx = 0;\n int cnt = 0;\n for(int i = n-1;i >= 0;i--) {\n if(s[i] == '*') {\n cnt++;\n }\n else {\n if(pq.size() < cnt) {\n pq.push({s[i], -i});\n }\n else {\n if(!pq.empty() and pq.top().first > s[i]) {\n pair<char, int> t = pq.top(); pq.pop();\n pq.push({s[i], -i});\n ans[-t.second] = t.first;\n }\n else {\n ans[i] = s[i];\n }\n }\n }\n }\n string final_ans = \"\";\n for(int i = 0;i < n;++i) {\n if(ans[i] != '.')\n final_ans += ans[i];\n }\n \n return final_ans;\n }\n};", "memory": "44965" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n= s.size();\n // make store every character index in stack\n vector<stack<int>> arr(26);\n vector<int> map(n+1,0);\n for(int i=0;i<n;i++)\n {\n if(s[i]!='*')\n {\n arr[s[i]-'a'].push(i);\n }\n else\n {\n // pop left most smallest char from set\n for(int j=0;j<26;j++)\n {\n if(!arr[j].empty())\n {\n int idx = arr[j].top();\n arr[j].pop();\n map[idx]++;\n break;\n }\n }\n }\n }\n\n // push all character which was not in map and not is '*'\n string res=\"\";\n for(int i=0;i<n;i++)\n {\n if(s[i]!='*' && map[i]==0)\n {\n res+=s[i];\n }\n }\n return res;\n }\n};", "memory": "47084" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.length();\n vector<stack<int>>v(26);\n vector<int>del(n,0);\n for(int i = 0;i < n;i++){\n if(s[i] != '*'){\n //cout << s[i] << endl;\n //cout << s[i] -'a' << endl;\n //cout << v[1].size() << endl;\n v[s[i] - 'a'].push(i);\n }\n else{\n del[i] = 1;\n for(int j = 0;j < 26;j++){\n if(v[j].size()){\n del[v[j].top()] = 1;\n v[j].pop();\n break;\n }\n }\n }\n }\n string ans = \"\";\n for(int i = 0;i < n;i++){\n if(!del[i]) ans += s[i];\n }\n return ans;\n }\n};", "memory": "47084" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n struct cmp{\n bool operator()(pair<char,int> &a, pair<char,int> &b)\n {\n if(a.first < b.first)\n return false;\n if(a.first == b.first)\n {\n return a.second < b.second;\n }\n return true;\n }\n };\n string clearStars(string s) {\n vector<int>v;\n priority_queue<pair<char,int>,vector<pair<char,int>>,cmp>pq;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]!='*')\n pq.push({s[i],i});\n else\n {\n auto p = pq.top();\n pq.pop();\n int ind = p.second;\n v.push_back(ind);\n v.push_back(i);\n }\n }\n // for(int i=0;i<v.size();i++)\n // {\n // cout<<v[i]<<\" \"<<endl;\n // }\n sort(v.begin(),v.end());\n int i = 0, j = 0;\n string ans=\"\";\n while(i<s.size() || j<v.size())\n {\n if(j<v.size() && i==v[j])\n {\n i++;\n j++;\n }\n else\n {\n ans.push_back(s[i]);\n i++;\n }\n }\n \n \n \n return ans;\n }\n};", "memory": "49203" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n vector<stack<int>> temp(26);\n vector<int> del;\n for(int i=0;i<s.length();i++){\n if(s[i]!='*') temp[s[i]-'a'].push(i);\n else {\n for(int j=0;j<26;j++){\n if(temp[j].size()){\n del.push_back(temp[j].top());\n temp[j].pop();\n break;\n }\n }\n }\n }\n sort(del.begin(),del.end());\n int j=0;\n string result=\"\";\n for(int i=0;i<s.length();i++){\n if(s[i]!='*' && (j>=del.size() || i!=del[j])) result.push_back(s[i]);\n else if(j<del.size() && i==del[j]) j++;\n }\n return result;\n }\n};", "memory": "49203" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n vector<vector<int>> idx(26);\n vector<int> remove;\n for(int i = 0; i < s.size(); i++)\n {\n if( s[i] == '*')\n {\n remove.push_back(i);\n for(int i = 0; i <26; i++)\n {\n if (idx[i].size() > 0)\n {\n auto temp = idx[i].back();\n idx[i].pop_back();\n remove.push_back(temp);\n break;\n }\n }\n }\n else\n {\n idx[ s[i] - 'a'].push_back(i);\n }\n }\n sort(remove.rbegin(), remove.rend());\n for(auto &r : remove)\n {\n //cout << r << endl;\n s.erase(r,1);\n }\n return s;\n }\n};", "memory": "51321" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n // Custom comparator to prioritize lexicographical order\n struct Cmp {\n bool operator()(const pair<char, int>& a, const pair<char, int>& b) const {\n if (a.first != b.first) {\n return a.first > b.first; // Lexicographically smaller characters come first\n }\n return a.second < b.second; // If characters are the same, keep their order\n }\n };\n\n string clearStars(string s) {\n int n = s.size();\n \n // Priority queue to maintain the lexicographically smallest characters\n priority_queue<pair<char, int>, vector<pair<char, int>>, Cmp> pq;\n vector<int> toEraseIndices; // To store indices of characters to be erased\n \n // Traverse through the string\n for (int i = 0; i < n; ++i) {\n if (s[i] == '*') {\n // Remove the smallest lexicographical character before the '*'\n if (!pq.empty()) {\n toEraseIndices.push_back(i); // Store '*' index\n toEraseIndices.push_back(pq.top().second); // Store the corresponding smallest char index\n pq.pop();\n }\n } else {\n // Add character with its index to the priority queue\n pq.push({s[i], i});\n }\n }\n \n // Sort indices to erase in descending order to avoid invalidating indices while erasing\n sort(toEraseIndices.rbegin(), toEraseIndices.rend());\n \n // Erase the characters from the string using stored indices\n for (int idx : toEraseIndices) {\n s.erase(s.begin() + idx);\n }\n \n return s;\n }\n};\n", "memory": "51321" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n priority_queue<pair<char,int>, vector<pair<char,int>>, greater<pair<char,int>> > pq;\n queue<int> q;\n int n=s.length();\n vector<int> dump;\n int i=0;\n while(i<n && s[i]=='*'){\n dump.push_back(i);\n i++;\n }\n for(i;i<n;i++){\n if(s[i]>='a' && s[i]<='z'){\n pq.push({s[i],n-1-i});\n }\n else{\n q.push(i);\n dump.push_back(q.front());\n q.pop();\n pair<char,int> x=pq.top();\n int index=x.second;\n dump.push_back(n-1-index);\n pq.pop();\n }\n\n }\n sort(dump.begin(),dump.end());\n int j=0;\n string answer;\n int m=dump.size();\n for(int i=0;i<n;i++){\n if(j<m && i==dump[j]){\n j++;\n }\n else{\n answer.push_back(s[i]);\n }\n }\n return answer;\n\n\n \n }\n};", "memory": "53440" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n typedef pair<char,int> PAIR;\n\n class Compare {\n public:\n bool operator()(PAIR a, PAIR b){\n if (a.first == b.first) return a.second > b.second;\n return a.first < b.first;\n }\n };\n\n string clearStars(string s) {\n int n = s.length();\n int lastStar = -1;\n vector<int> countStar(n+1, 0);\n for (int i = n-1; i >= 0; i--) {\n countStar[i] = countStar[i+1];\n if (s[i] == '*') {\n if (lastStar == -1) lastStar = i;\n countStar[i]++;\n }\n }\n if (countStar[0] == 0) return s;\n\n priority_queue<PAIR, vector<PAIR>, Compare> pq;\n\n for (int i = lastStar-1; i >= 0; i--) {\n if (s[i] != '*') {\n pq.push({s[i], i});\n if (pq.size() > countStar[i]) pq.pop();\n }\n }\n\n priority_queue<int> listIndex;\n while (!pq.empty()) {\n PAIR p = pq.top();\n // cout << p.first << \" \" << p.second << endl;\n listIndex.push(p.second);\n pq.pop();\n }\n\n while (!listIndex.empty()) {\n s.erase(listIndex.top(), 1);\n listIndex.pop();\n }\n\n s.erase(remove(s.begin(), s.end(), '*'), s.end());\n\n return s;\n }\n};", "memory": "55559" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n map<char, vector<int>> mp;\n set<char> st;\n \n vector<int> v(n, 1);\n st.insert(s[0]);\n mp[s[0]].push_back(0);\n \n for(int i=1; i<n; i++) {\n if(s[i]=='*') {\n auto it = st.begin();\n char c = *it;\n int ind = mp[c].back();\n mp[c].pop_back();\n v[ind]=0, v[i]=0;\n if(!mp[c].size()) st.erase(it);\n \n } else {\n st.insert(s[i]);\n mp[s[i]].push_back(i);\n }\n }\n \n string res = \"\";\n \n for(int i=0; i<n; i++) {\n if(v[i]) res+=s[i];\n }\n \n return res;\n }\n};", "memory": "57678" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n string ans = \"\";\n unordered_map<char, vector<int>> m;\n unordered_set<int> indices;\n bool found = false;\n for (int i = 0; i < s.size(); ++i)\n {\n if (s[i] != '*')\n {\n m[s[i]].push_back(i);\n indices.insert(i);\n }\n else\n {\n found = true;\n for (auto c = 'a'; c <= 'z'; ++c)\n {\n if (!m[c].empty())\n {\n auto idx = m[c].back();\n indices.erase(idx);\n m[c].pop_back();\n break;\n }\n }\n \n }\n }\n if (!found)\n return s;\n for (int i = 0; i < s.size(); ++i)\n {\n if (s[i] != '*' && indices.find(i) != indices.end())\n ans.push_back(s[i]);\n }\n return ans;\n }\n};", "memory": "59796" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n vector<bool> bdel(n, false);\n\n priority_queue<vector<int>> mq;\n\n string ans;\n\n for (int i = 0; i < n; i++) {\n if (s[i] == '*') {\n bdel[i] = true;\n int pos = mq.top()[1];\n bdel[pos] = true;\n mq.pop();\n } else {\n mq.push({26-s[i]-'a', i});\n }\n }\n\n for (int i = 0; i < n; i++) {\n if (!bdel[i])\n ans += s[i];\n }\n\n return ans;\n }\n};", "memory": "61915" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n unordered_map<int,vector<int>> m;\n for(int i=0;i<s.size();i++){\n if(s[i]=='*'){\n for(int i=0;i<26;i++){\n if(m.find(i)!=m.end()){\n int a=m[i][m[i].size()-1];\n m[i].pop_back();\n s[a]='@';\n if(m[i].size()==0) m.erase(i);\n break;\n }\n }\n }\n else{\n m[s[i]-'a'].push_back(i);\n }\n\n }\n string k=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]!='@'&&s[i]!='*') k.push_back(s[i]);\n }\n return k;\n }\n};", "memory": "64034" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n unordered_map<char,vector<int>>mapping;\n string ans;\n for(int i=0;i<s.size();i++){\n if(s[i]=='*'){\n for(int j=0;j<26;j++){\n if(mapping.find('a'+j)!=mapping.end()){\n s[mapping['a'+j].back()]='-';\n mapping['a'+j].pop_back();\n if(mapping['a'+j].size()==0)\n mapping.erase('a'+j);\n break;\n }\n }\n }\n else mapping[s[i]].push_back(i);\n }\n for(int i=0;i<s.size();i++){\n if(s[i]!='*'&&s[i]!='-')\n ans.push_back(s[i]);\n }\n return ans;\n }\n};", "memory": "66153" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n map<char, priority_queue <int>> mp;\n int n = s.size();\n\n for(int i = 0; i < n; i++) {\n if(s[i] != '*') {\n mp[s[i]].push(i);\n } else {\n while(!mp.empty() && mp.begin()->second.size() == 0) {\n mp.erase(mp.begin());\n }\n\n if(mp.empty()) {\n continue;\n }\n\n (mp.begin()->second).pop();\n }\n }\n\n vector<int> v;\n for(auto i : mp) {\n priority_queue<int> pq = i.second;\n while(!pq.empty()) {\n v.push_back(pq.top());\n pq.pop();\n }\n }\n\n sort(v.begin(), v.end());\n\n string ans = \"\";\n for(auto i : v) {\n ans += s[i];\n }\n\n return ans;\n }\n};", "memory": "68271" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n std::list<char> str;\n std::vector<std::vector<list<char>::iterator>> posMap(26);\n\n for (const auto& c : s) {\n if (c == '*') {\n for (int i = 0; i < 26; ++i) {\n if (posMap[i].size() != 0) {\n auto listItr = posMap[i].back();\n posMap[i].pop_back();\n str.erase(listItr);\n break;\n }\n } \n } else {\n str.emplace_back(c);\n auto itr = --str.end();\n posMap[c - 'a'].emplace_back(itr);\n }\n }\n\n string result(str.begin(), str.end());\n return result;\n }\n};", "memory": "70390" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n std::list<char> str;\n std::vector<std::vector<list<char>::iterator>> posMap(26);\n\n for (const auto& c : s) {\n if (c == '*') {\n for (int i = 0; i < 26; ++i) {\n if (posMap[i].size() != 0) {\n auto listItr = posMap[i].back();\n posMap[i].pop_back();\n str.erase(listItr);\n break;\n }\n } \n } else {\n str.emplace_back(c);\n auto itr = --str.end();\n posMap[c - 'a'].emplace_back(itr);\n }\n }\n\n string result(str.begin(), str.end());\n return result;\n }\n};", "memory": "70390" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n std::list<char> str;\n std::vector<std::vector<list<char>::iterator>> posMap(26);\n\n for (const auto& c : s) {\n if (c == '*') {\n for (int i = 0; i < 26; ++i) {\n if (posMap[i].size() != 0) {\n auto listItr = posMap[i].back();\n posMap[i].pop_back();\n str.erase(listItr);\n break;\n }\n } \n } else {\n str.emplace_back(c);\n auto itr = --str.end();\n posMap[c - 'a'].emplace_back(itr);\n }\n }\n\n string result(str.size(), ' ');\n std::copy(str.begin(), str.end(), result.begin());\n\n return result;\n }\n};", "memory": "72509" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n std::list<char> str;\n std::vector<std::vector<list<char>::iterator>> posMap(26);\n\n for (const auto& c : s) {\n if (c == '*') {\n for (int i = 0; i < 26; ++i) {\n if (posMap[i].size() != 0) {\n auto listItr = posMap[i].back();\n posMap[i].pop_back();\n str.erase(listItr);\n break;\n }\n } \n } else {\n str.emplace_back(c);\n auto itr = --str.end();\n posMap[c - 'a'].emplace_back(itr);\n }\n }\n\n string result(str.size(), ' ');\n std::copy(str.begin(), str.end(), result.begin());\n\n return result;\n }\n};", "memory": "72509" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n std::list<char> str;\n std::vector<std::vector<list<char>::iterator>> posMap(26);\n\n for (const auto& c : s) {\n if (c == '*') {\n for (int i = 0; i < 26; ++i) {\n if (posMap[i].size() != 0) {\n auto listItr = posMap[i].back();\n posMap[i].pop_back();\n str.erase(listItr);\n break;\n }\n } \n } else {\n str.emplace_back(c);\n auto itr = --str.end();\n posMap[c - 'a'].emplace_back(itr);\n }\n }\n\n string result(str.size(), ' ');\n std::copy(str.begin(), str.end(), result.begin());\n\n return result;\n }\n};", "memory": "74628" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n std::list<char> str;\n std::vector<std::vector<list<char>::iterator>> posMap(26);\n\n for (const auto& c : s) {\n if (c == '*') {\n for (int i = 0; i < 26; ++i) {\n if (posMap[i].size() != 0) {\n auto listItr = posMap[i].back();\n posMap[i].pop_back();\n str.erase(listItr);\n break;\n }\n } \n } else {\n str.emplace_back(c);\n auto itr = --str.end();\n posMap[c - 'a'].emplace_back(itr);\n }\n }\n\n string result(str.size(), ' ');\n std::copy(str.begin(), str.end(), result.begin());\n\n return result;\n }\n};", "memory": "74628" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n unordered_map<int , vector<int>> mp;\n unordered_set<int> indices;\n int n = s.size();\n for(int i=0; i<n; i++){\n if (s[i]=='*'){\n for(int j=0; j<26; j++){\n if(mp[j].size() > 0){\n int idx = mp[j][mp[j].size()-1];\n mp[j].pop_back();\n indices.insert(idx);\n break;\n }\n }\n }\n else{\n mp[int(s[i])-97].push_back(i);\n }\n }\n string res = \"\";\n for(int i=0; i<n; i++){\n if(s[i]=='*' || indices.find(i)!=indices.end()) continue;\n res+=s[i];\n }\n return res;\n }\n};", "memory": "76746" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n unordered_map<int,int>visited;\n map<char,vector<int>>mp;\n priority_queue <char, vector<char>, greater<char>> pq;\n for(int i=0;i<s.size();i++){\n if(s[i]=='*'){\n visited[mp[pq.top()].back()]++;\n mp[pq.top()].pop_back();\n pq.pop();\n }\n else{\n mp[s[i]].push_back(i);\n pq.push(s[i]);\n }\n }\n string t=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]=='*'||visited.find(i)!=visited.end())continue;\n t+=s[i];\n }\n return t;\n }\n};", "memory": "78865" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n=s.size();\n stack<int> st;\n unordered_map<int,vector<int>> mp;\n unordered_map<int,int> del;\n vector<int> v(26,0);\n for(int i=0;i<n;i++){\n if(s[i]!='*'){\n mp[s[i]-'a'].push_back(i);\n }\n else{\n int f=1;\n for(int i=0;i<26;i++){\n if(mp[i].size()>0 && f==1){\n del[mp[i].back()]++;\n mp[i].pop_back();\n f=0;\n }\n }\n }\n }\n\n string ans;\n for(int i=0;i<n;i++){\n if(del.find(i)==del.end() && s[i]!='*'){\n ans.push_back(s[i]);\n }\n }\n return ans;\n }\n};", "memory": "80984" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n struct cmp{\n bool operator()(pair<char,int> a, pair<char,int> b){\n if(a.first == b.first)\n return a.second < b.second;\n else\n return a.first > b.first;\n }\n };\n string clearStars(string s) {\n unordered_map<int,int> mp;\n priority_queue<pair<char,int>, vector<pair<char,int>>, cmp> pq;\n for(int i = 0 ; i < s.size() ; i++){\n if(s[i] >= 'a' and s[i] <= 'z')\n pq.push({s[i], i});\n else{\n char ch = pq.top().first;\n int del = pq.top().second;\n pq.pop();\n mp[del] = 1;\n }\n }\n string ans = \"\";\n for(int i = 0 ; i < s.size() ; i++){\n if(mp.find(i) == mp.end() and s[i] != '*')\n ans.push_back(s[i]);\n }\n return ans;\n }\n};", "memory": "83103" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n struct cmp{\n bool operator()(pair<char,int>&a,pair<char,int>&b){\n if(a.first==b.first) return a.second<b.second;\n return a.first>b.first;\n }\n };\n string clearStars(string s) {\n priority_queue<pair<char,int>,vector<pair<char,int>>,cmp> q;\n unordered_set<int>set;\n for(int i=0;i<s.size();i++){\n if(s[i]=='*'){\n set.insert(q.top().second);\n q.pop();\n }\n else{\n q.push(make_pair(s[i],i));\n }\n }\n string ans;\n for(int i=0;i<s.size();i++){\n if(!set.count(i)&&s[i]!='*')ans+=s[i];\n }\n return ans;\n }\n};", "memory": "85221" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\nstruct Compare {\n bool operator()(const pair<char, int>& p1, const pair<char, int>& p2) {\n if (p1.first == p2.first) {\n // If first characters are the same, sort by second element in descending order\n return p1.second < p2.second;\n }\n // Otherwise, sort by first element (character) in ascending order\n return p1.first > p2.first;\n }\n};\n string clearStars(string s) {\n priority_queue<pair<char,int>,vector<pair<char,int>>,Compare>pq;\n unordered_set<int>st;\n for(int i=0;i<s.size();i++){\n if(s[i]=='*'){\n if(!pq.empty()){\n st.insert(pq.top().second);\n cout<<pq.top().second;\n pq.pop();\n }\n }else{\n pq.push({s[i],i});\n }\n }\n string ans=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]!='*'){\n if(st.find(i)==st.end()){\n ans+=s[i];\n }\n }\n }\n return ans;\n }\n};", "memory": "87340" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.length();\n unordered_set<int> removedIdx;\n \n priority_queue<pair<int, int>> pq; \n for (int i = 0; i < n; i ++) {\n if (s[i] == '*') {\n removedIdx.insert(pq.top().second);\n pq.pop();\n } else {\n pq.push({'z'-s[i], i});\n }\n }\n\n string clearedStr;\n for (int i = 0; i < n; i ++) {\n if (s[i] == '*' || removedIdx.find(i) != removedIdx.end()) {\n continue;\n }\n clearedStr += s[i];\n }\n return clearedStr;\n }\n};", "memory": "89459" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n vector<vector<int>>ok(26);\n set<int>st;\n for(int i = 0;i<s.size();i++){\n if(s[i]=='*'){\n for(int j=0;j<26;j++){\n if(ok[j].size()>0){\n st.insert(ok[j].back());\n ok[j].pop_back();\n break;\n }\n }\n }else{\n ok[s[i]-'a'].push_back(i);\n }\n }\n string ans = \"\";\n for(int i = 0;i<s.size();i++){\n if(st.count(i) == false && s[i]!='*'){\n ans+=s[i];\n }\n }\n return ans;\n }\n};", "memory": "91578" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\nint n=s.size();\n set<int>st;\nvector<vector<int>>mp(26);\n for(int i=0;i<s.size();i++){\n if(s[i]!='*') mp[s[i]-'a'].push_back(i);\n else{\n char ref='a';\n for(int i=0;i<26;i++){\n if(mp[i].size()>0) {\n ref='a'+i;\n break;\n }\n }\n\n st.insert((*--mp[ref-'a'].end()));\n mp[ref-'a'].pop_back();\n }\n }\n string ans;\n\n for(int i=0;i<n;i++){\n if(s[i]!='*' && st.find(i)==st.end()) ans+=s[i];\n }\n return ans;\n }\n};", "memory": "93696" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n vector<vector<int>>ok(26);\n set<int>st;\n for(int i = 0;i<s.size();i++){\n if(s[i]=='*'){\n for(int j=0;j<26;j++){\n if(ok[j].size()>0){\n st.insert(ok[j].back());\n ok[j].pop_back();\n break;\n }\n }\n }else{\n ok[s[i]-'a'].push_back(i);\n }\n }\n string ans = \"\";\n for(int i = 0;i<s.size();i++){\n if(st.count(i) == false && s[i]!='*'){\n ans+=s[i];\n }\n }\n return ans;\n }\n};", "memory": "95815" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n vector<vector<int > > hash(26);\n set<int > remove;\n\n for(int i = 0; i < s.size(); i++) {\n if(s[i] == '*')\n {\n for(int j = 0; j < 26; j++)\n {\n if(hash[j].size() > 0)\n {\n remove.insert(hash[j].back());\n hash[j].pop_back();\n break;\n }\n }\n }\n else\n {\n hash[s[i] - 'a'].push_back(i);\n }\n }\n\n string str;\n for(int i = 0; i < s.size(); i++)\n {\n if(s[i] != '*' && remove.find(i) == remove.end())\n {\n str.push_back(s[i]);\n }\n }\n\n return str;\n }\n};", "memory": "97934" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution { \npublic:\n string clearStars(string s) {\n map<char,vector<int>> mp;\n for(int i=0;i<s.size();i++){\n if(s[i]=='*'){\n int index = mp.begin()->second.back();\n mp.begin()->second.pop_back();\n s[index]='*';\n if(mp.begin()->second.size()==0){\n mp.erase(mp.begin());\n }\n }\n else{\n mp[s[i]].push_back(i);\n }\n }\n string ans=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]!='*'){\n ans.push_back(s[i]);\n }\n }\n return ans;\n }\n};", "memory": "100053" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n string output = \"\";\n map<char, vector<int>> lastseen; // < char, lastseen index >\n \n int l = 0; int r = 0;\n \n while(r < s.length()){\n if(s[r] == '*'){\n s[r] = '$';\n int ind = lastseen.begin()->second.back();\n s[ind] = '$';\n lastseen.begin()->second.pop_back();\n if(lastseen.begin()->second.empty()){\n lastseen.erase(lastseen.begin());\n }\n } else {\n lastseen[s[r]].push_back(r);\n }\n r++;\n }\n for(int i = 0; i< s.size(); i++){\n if(s[i] != '$'){\n output.push_back(s[i]);\n }\n }\n return output;\n }\n};", "memory": "102171" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n=s.size();\n map<char,vector<int>> mcv;\n for(int i=0;i<n;++i)\n {\n if(s[i]=='*')\n {\n auto it=mcv.begin();\n s[it->second.back()]='*';\n it->second.pop_back();\n if(it->second.empty()) mcv.erase(it);\n }\n else\n {\n mcv[s[i]].push_back(i);\n }\n }\n string a;\n for(char c:s)\n {\n if(c!='*') a+=c;\n }\n return a;\n }\n};", "memory": "104290" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n=s.length();\n int mini=-1,ind=-1;\n map<char,vector<int>> mp;\n vector<int> v(26);\n\n for(int i=0;i<n;i++)\n {\n if(s[i]=='*')\n {\n if(mp.size())\n {\n auto it=mp.begin();\n int size=it->second.size();\n s[it->second[size-1]]='*';\n it->second.pop_back();\n if(it->second.size()==0)\n mp.erase(it);\n }\n }\n else\n mp[s[i]].push_back(i);\n }\n string ans=\"\";\n for(int i=0;i<n;i++)\n {\n if(s[i]!='*')\n ans+=s[i];\n }\n return ans;\n } \n};", "memory": "106409" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n=s.length();\n int mini=-1,ind=-1;\n map<char,vector<int>> mp;\n vector<int> v(26);\n\n for(int i=0;i<n;i++)\n {\n if(s[i]=='*')\n {\n if(mp.size())\n {\n auto it=mp.begin();\n int size=it->second.size();\n s[it->second[size-1]]='*';\n it->second.pop_back();\n if(it->second.size()==0)\n mp.erase(it);\n }\n }\n else\n mp[s[i]].push_back(i);\n }\n string ans=\"\";\n for(int i=0;i<n;i++)\n {\n if(s[i]!='*')\n ans+=s[i];\n }\n return ans;\n \n }\n};", "memory": "108528" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n map<int,vector<int>> mp;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]!='*') {mp[s[i]-'a'].push_back(i); continue;}\n if(mp.begin()->second.size()==1) {s[mp.begin()->second[0]]='.';mp.erase(mp.begin());}\n else{s[mp.begin()->second.back()]='.';mp.begin()->second.pop_back();}\n }\n string ans=\"\";\n for(auto i: s){\n if(i!='.' && i!='*') ans+=i;\n }\n return ans;\n }\n};", "memory": "110646" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n \n map<char,vector<int>> char_idx_map;\n vector<bool> del(s.size(),false);\n\n for(int i=0;i<s.size();i++)\n {\n if(s[i] == '*')\n {\n vector<int>& min_ch_idxs = char_idx_map.begin()->second;\n\n int del_idx = min_ch_idxs.back();\n\n del[del_idx] = true;\n\n min_ch_idxs.pop_back();\n\n if(min_ch_idxs.empty())\n char_idx_map.erase(char_idx_map.begin());\n\n }\n \n else\n {\n char_idx_map[s[i]].push_back(i);\n }\n }\n\n string res;\n\n for(int i=0;i<s.size();i++)\n {\n if(s[i]!='*' && !del[i])\n res.push_back(s[i]);\n }\n\n return res;\n }\n};", "memory": "110646" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n map<char, vector<int>> mp;\n int n = s.length();\n \n for(int i = 0; i < n; i++) {\n if(s[i] == '*') {\n auto it = mp.begin();\n if(it == mp.end()) {\n continue;\n }\n it->second.pop_back();\n if(it->second.size() == 0) {\n mp.erase(it->first);\n }\n } else {\n mp[s[i]].push_back(i);\n }\n }\n vector<int> all_good_index;\n \n for(auto& [c, idxes] : mp) {\n for(int x : idxes) {\n all_good_index.push_back(x);\n }\n }\n sort(all_good_index.begin(), all_good_index.end());\n \n // for(int x : all_good_index) {\n // cout << x << \" \";\n // }\n // cout << endl;\n int ans_size = all_good_index.size();\n string Nothing(ans_size, ' ');\n \n for(int i = 0; i < ans_size; i++) {\n Nothing[i] = s[all_good_index[i]];\n }\n \n \n return Nothing;\n }\n};\n\n/*\n map<char, int> mp = {\n a: {0, 1}\n b: {2}\n \n }\n \n 0 1 2 3 4\n a a b a *\n ^\n \n ans = \"aab\"\n \n\n*/", "memory": "112765" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class cmp{\n public:\n bool operator()(pair<char,int> a , pair<char,int> b){\n if(a.first==b.first){\n return a.second<b.second;\n }\n return a.first>b.first;\n }\n\n};\nclass Solution {\npublic:\n string clearStars(string s) {\n\n priority_queue<pair<char,int> , vector<pair<char,int>> , cmp>pq;\n set<int> st;\n\n\n for(int i =0;i<s.size();i++){\n if(s[i]=='*'){\n if(!pq.empty()){\n auto it = pq.top();\n int idx = it.second;\n st.insert(idx);\n pq.pop();\n }\n\n }\n else{\n pq.push({s[i],i});\n }\n }\n\n string ans = \"\";\n for(int i =0;i<s.size();i++){\n if(s[i]!='*'){\n if(st.find(i)==st.end()){\n ans.push_back(s[i]);\n }\n }\n }\n return ans;\n \n \n }\n};", "memory": "112765" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n=s.size();\n string ans=\"\",an=\"\";\n map<char,vector<int>>mp;\n for(int i=0;i<n;i++)\n {\n if(s[i]=='*')\n {\n auto it=mp.begin();\n ans[(*it).second[((*it).second.size()-1)]]='-';\n (*it).second.pop_back();\n if((*it).second.size()==0) mp.erase(it); \n ans.push_back('-');\n }\n else\n {\n ans.push_back(s[i]);\n mp[s[i]].push_back(i);\n }\n }\n for(auto x:ans) \n {\n if(x!='-') an.push_back(x);\n }\n return an;\n }\n};", "memory": "114884" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n set<pair<char,int>> st;\n string res;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]=='*')\n {\n pair<char,int>p =*st.begin();\n int del=-(p.second);\n s[del-1]='*';\n st.erase(st.begin());\n }\n else\n st.insert({s[i],(i+1)*-1});\n }\n cout<<s<<endl;\n for(auto v:st)\n cout<<v.first<<\" \"<<v.second<<endl;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]!='*')res+=s[i];\n }\n return res;\n }\n};", "memory": "117003" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n set<pair<char,int>> st;\n string res;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]=='*')\n {\n pair<char,int>p =*st.begin();\n int del=-(p.second);\n s[del-1]='*';\n st.erase(st.begin());\n }\n else\n st.insert({s[i],(i+1)*-1});\n }\n cout<<s<<endl;\n for(auto v:st)\n cout<<v.first<<\" \"<<v.second<<endl;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]!='*')res+=s[i];\n }\n return res;\n }\n};", "memory": "117003" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n=s.length();\n map<char,priority_queue<int>>m;\n for(int i=0;i<n;i++)\n {\n if(s[i]!='*') m[s[i]].push(i);\n else\n {\n auto x=m.begin();\n s[x->second.top()]='0';\n x->second.pop();\n if(x->second.empty())\n {\n m.erase(x->first);\n }\n }\n }\n string ans=\"\";\n for(int i=0;i<n;i++) if(s[i]!='0' and s[i]!='*') ans+=s[i];\n return ans;\n }\n};", "memory": "119121" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n map<char, priority_queue<int>>mp;\n for(int i = 0; i < s.length(); i++)\n {\n if(s[i] == '*')\n {\n auto it = mp.begin();\n s[it->second.top()] = '*';\n it->second.pop();\n if(it->second.size() == 0)\n {\n mp.erase(it);\n }\n }\n else\n {\n mp[s[i]].push(i);\n }\n }\n string ans = \"\";\n for(int i = 0; i < s.length(); i++)\n {\n if(s[i] != '*')ans.push_back(s[i]);\n }\n return ans;\n }\n};", "memory": "119121" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n map<char,vector<int>> mp;\n int n= s.size();\n vector<int> v(n,0);\n for(int i =0 ;i <s.size(); i++){\n if(s[i]!='*'){\n mp[s[i]].push_back(i);\n }\n else{\n v[i] = 1;\n for(auto &x : mp){\n int m = x.second.size();\n v[x.second[m-1]] = 1;\n x.second.pop_back();\n if(x.second.size()==0) mp.erase(x.first);\n break;\n }\n }\n }\n string ans =\"\";\n for(int i = 0; i<n; i++){\n if(v[i]!=1) ans+=s[i];\n }\n return ans;\n }\n};", "memory": "121240" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n map<char, vector<int>> mp;\n int n = s.size();\n vector<int> blocked(n + 1, 0);\n for(int i = 0; i < s.size(); i ++) {\n if(s[i] == '*') {\n blocked[i] = 1;\n if(mp.size()) {\n int id = mp.begin() -> second.back();\n mp.begin() -> second.pop_back();\n blocked[id] = 1;\n if(mp.begin() -> second.size() == 0) {\n mp.erase(mp.begin());\n }\n }\n } else mp[s[i]].push_back(i);\n\n }\n string ans = \"\";\n for(int i = 0; i < n; i ++) {\n if(!blocked[i]) {\n ans += s[i];\n }\n }\n return ans;\n }\n};", "memory": "121240" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n map<char,vector<int>> mp;\n\n vector<int> del;\n for(int i = 0 ; i < n ; i++){\n if(s[i] != '*') mp[s[i]].push_back(i);\n else{\n auto it = mp.begin();\n \n del.push_back(it->second.back());\n it->second.pop_back();\n if(it->second.empty()) mp.erase(it);\n \n }\n } \n\n sort(del.begin() , del.end());\n \n string res = \"\";\n int j = 0;\n for(int i = 0 ; i < n ; i++){\n if(s[i] == '*') continue;\n else if(j < del.size() && i == del[j]){\n j++;\n }else res += s[i];\n }\n\n return res;\n }\n};", "memory": "123359" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.length();\n multiset<char> ms;\n unordered_map<char,vector<int>> hashmap;\n for(int i=0;i<n;i++) {\n char c = s[i];\n if(c == '*') {\n if(!ms.empty()) {\n auto it = ms.begin();\n hashmap[*it].pop_back();\n ms.erase(it);\n } \n }\n else {\n auto it = ms.insert(c);\n hashmap[c].push_back(i);\n }\n }\n\n vector<int> positions;\n for(auto &it: hashmap) {\n for(auto &pos: it.second)\n positions.push_back(pos);\n }\n sort(positions.begin(),positions.end());\n string result(positions.size(),'a');\n int start=0;\n for(auto &pos: positions) {\n result[start++] = s[pos];\n }\n return result;\n }\n};\n\n// ab*de*f\n\n\n", "memory": "123359" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n unordered_map<char,set<int>> pos;\n unordered_map<char,int> mp;\n\n for(int i=0;i<s.size();i++){\n if(s[i]!='*'){\n mp[s[i]]++;\n pos[s[i]].insert(i);\n }\n else{\n for(char c='a';c<='z';c++){\n if(mp.find(c)!=mp.end()&&mp[c]>0){\n mp[c]--;\n auto it=pos[c].end();\n it--;\n\n pos[c].erase(it);\n break;\n }\n }\n }\n }\n\n string ans=\"\";\n\n for(int i=0;i<s.size();i++) ans+='#';\n\n for(char c='a';c<='z';c++){\n for(auto ind: pos[c]){\n ans[ind]=c;\n }\n }\n\n string res=\"\";\n\n for(int i=0;i<ans.size();i++){\n if(ans[i]!='#') res+=ans[i];\n }\n\n return res;\n }\n};", "memory": "125478" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n vector<int> vs(n, 0);\n set<pair<char, int>> st;\n for(int i = 0; i < n; i++) {\n if(s[i] == '*') {\n if(!st.empty()) {\n pair<char, int> p = *st.begin();\n st.erase(st.begin());\n vs[-p.second] = 1;\n }\n vs[i] = 1;\n }\n else {\n st.insert({s[i], -i});\n }\n }\n string ans;\n for(int i = 0; i < n; i++) {\n if(vs[i] == 0) {\n ans += s[i];\n }\n }\n return ans;\n }\n};", "memory": "127596" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n set<pair<int,int>> st;\n int n=s.size();\n vector<int> mi(s.size(),0);\n for(int i=0;i<s.size();i++){\n if(s[i]=='*'){\n if(!st.empty()){\n pair<int,int> cur=*st.begin();\n mi[-cur.second]=1;\n st.erase(st.begin());\n }\n continue;\n }\n st.insert({s[i]-'a',-i});\n }\n string ans=\"\";\n for(int i=0;i<n;i++){\n if(!mi[i] && s[i]!='*'){\n ans+=s[i];\n }\n }\n return ans;\n\n }\n};", "memory": "127596" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n vector<int> v(n,0);\n map<int,priority_queue<int>> mp;\n for(int i=0; i<n; i++){\n if(s[i] == '*'){\n v[i] = 1;\n auto x = mp.begin();\n v[(x->second).top()] = 1;\n (x->second).pop();\n if((x->second).empty()){\n mp.erase(x->first);\n }\n }\n else{\n mp[s[i]-'a'].push(i);\n }\n }\n string res = \"\";\n for(int i=0 ; i<n; i++){\n if(v[i]==0){\n res.push_back(s[i]);\n }\n }\n return res;\n }\n};", "memory": "129715" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n vector<int> v(n,0);\n map<int,priority_queue<int>> mp;\n string res = \"\";\n for(int i=0; i<n; i++){\n if(s[i] != '*'){\n mp[s[i]-'a'].push(i);\n continue;\n }\n v[i] = 1;\n auto x = mp.begin();\n v[x->second.top()] = 1;\n x->second.pop();\n if(x->second.empty()){\n mp.erase(x->first);\n }\n }\n for(int i=0 ; i<n; i++){\n if(v[i]==0){\n res.push_back(s[i]);\n }\n }\n return res;\n }\n};", "memory": "129715" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n vector<set<int>> alphaIndicesMap(26);\n int iter = 0, n = s.length();\n vector<int> arrIndicesAsterisk;\n for(char& c : s)\n {\n if(isalpha(c)) alphaIndicesMap[c-'a'].insert(iter++);\n else arrIndicesAsterisk.push_back(iter++);\n }\n for(auto& idx : arrIndicesAsterisk)\n {\n bool status = false;\n for(int i = 0; i < 26; i++)\n {\n if(alphaIndicesMap[i].empty()) continue;\n auto it = alphaIndicesMap[i].lower_bound(idx);\n if(it == alphaIndicesMap[i].end())\n {\n it = prev(it);\n int idx2 = *it;\n if(idx2 < idx)\n {\n alphaIndicesMap[i].erase(it);\n status = true;\n break;\n }\n }\n else\n {\n if(it != alphaIndicesMap[i].begin())\n {\n it = prev(it);\n alphaIndicesMap[i].erase(it);\n status = true;\n break;\n }\n }\n }\n }\n string temp(s.length(), '0');\n for(int i = 0; i < 26; i++)\n {\n for(auto& p : alphaIndicesMap[i])\n temp[p] = (char)(i + 'a');\n }\n string res;\n for(char& c : temp)\n {\n if(c == '0') continue;\n res += c;\n }\n return res;\n }\n};", "memory": "131834" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n stack<int>st[26];\n map<int,int>has;\n for (int i=0; i<s.length(); i++) {\n if (s[i]!='*') {\n st[s[i] - 'a'].push(i);\n }\n else {\n bool flag = 0;\n for (int i=0; i<26 && flag==0; i++) {\n if (st[i].empty()==0) {\n has[st[i].top()] = 1;\n st[i].pop();\n flag = 1;\n }\n }\n }\n }\n string ans = \"\";\n for (int i=0; i<s.length(); i++) {\n if (s[i]=='*' || has[i]) continue;\n ans += s[i];\n }\n return ans;\n }\n};", "memory": "133953" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Comparator {\n public:\n bool operator()(const vector<int>&a, const vector<int>&b){\n return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0];\n }\n};\nclass Solution {\npublic:\n string clearStars(string s) {\n int n = s.length();\n vector<bool>toDiscard(n, false);\n priority_queue<vector<int>, vector<vector<int>>, Comparator>pq;\n for(int i=0; i<n; i++) {\n if (s[i] == '*'){\n toDiscard[i] = true;\n if (!pq.empty()) {\n auto it = pq.top();\n pq.pop();\n toDiscard[it[1]] = true;\n }\n } else {\n pq.push({s[i]-'a', i});\n }\n }\n string ans = \"\";\n for(int i=0; i<n; i++){\n if (!toDiscard[i]){\n ans += s[i];\n }\n }\n return ans;\n }\n};", "memory": "136071" }