Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Add Binary
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution
{
public:
string addBinary(string a, string b)
{
int n1 = a.length();
int n2 = b.length();
int carry = 0;
string s = "";
while (n1 > 0 || n2 > 0)
{
int sum = 0;
if (n1 > 0)
{
sum += a[n1 - 1] - '0';
n1--;
}
if (n2 > 0)
{
sum += b[n2 - 1] - '0';
n2--;
}
sum += carry;
switch (sum)
{
case 0:
carry = 0;
s.push_back('0');
break;
case 1:
carry = 0;
s.push_back('1');
break;
case 2:
carry = 1;
s.push_back('0');
break;
case 3:
carry = 1;
s.push_back('1');
break;
}
}
if (carry != 0)
{
s.push_back('1');
}
reverse(s.begin(), s.end());
return s;
}
};
16 changes: 16 additions & 0 deletions Final Value of Variable After Performing Operations
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int finalValueAfterOperations(vector& operations) {

int res = 0 ;

for( auto operation : operations ) {
if( operation[1] == '+' )
res++ ;
else
res-- ;
}

return res ;
}
};
22 changes: 22 additions & 0 deletions Sort Characters By Frequency
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
string frequencySort(string s) {
unordered_map<char,int> mp;
for(int i= 0;i< s.length(); i++){
mp[s[i]]++;
}
priority_queue<pair<int,char>>pq;
for(auto i : mp){
pq.push({i.second,i.first});
}
string ans = "";
while(!pq.empty()){
pair<int,char> p = pq.top();
pq.pop();
while(p.first--){
ans+=p.second;
}
}
return ans;
}
};