diff --git a/Add Binary b/Add Binary new file mode 100644 index 0000000..5ec9b8b --- /dev/null +++ b/Add Binary @@ -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; + } +}; diff --git a/Final Value of Variable After Performing Operations b/Final Value of Variable After Performing Operations new file mode 100644 index 0000000..0e0eb48 --- /dev/null +++ b/Final Value of Variable After Performing Operations @@ -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 ; +} +}; diff --git a/Sort Characters By Frequency b/Sort Characters By Frequency new file mode 100644 index 0000000..ec58f0b --- /dev/null +++ b/Sort Characters By Frequency @@ -0,0 +1,22 @@ +class Solution { +public: + string frequencySort(string s) { + unordered_map mp; + for(int i= 0;i< s.length(); i++){ + mp[s[i]]++; + } + priority_queue>pq; + for(auto i : mp){ + pq.push({i.second,i.first}); + } + string ans = ""; + while(!pq.empty()){ + pair p = pq.top(); + pq.pop(); + while(p.first--){ + ans+=p.second; + } + } + return ans; + } +};