From b6bb8782169bc0287a404780ee17ea24191633ef Mon Sep 17 00:00:00 2001 From: fabianrobin123 <115490451+fabianrobin123@users.noreply.github.com> Date: Mon, 10 Oct 2022 22:03:10 +0530 Subject: [PATCH 1/3] Add Binary Given two binary strInput: a = "11", b = "1" Output: "100"ings a and b, return their sum as a binary string. --- Add Binary | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Add Binary 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; + } +}; From cf1663227578d64fe994993e269d31bc68e1b5e2 Mon Sep 17 00:00:00 2001 From: fabianrobin123 <115490451+fabianrobin123@users.noreply.github.com> Date: Mon, 10 Oct 2022 22:06:32 +0530 Subject: [PATCH 2/3] Final Value of Variable After Performing Operations --- ...Value of Variable After Performing Operations | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Final Value of Variable After Performing Operations 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 ; +} +}; From 47e4f63ba986cd5596cda10fd48f5020f6e9f703 Mon Sep 17 00:00:00 2001 From: fabianrobin123 <115490451+fabianrobin123@users.noreply.github.com> Date: Mon, 10 Oct 2022 22:07:47 +0530 Subject: [PATCH 3/3] Sort Characters By Frequency --- Sort Characters By Frequency | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Sort Characters By Frequency 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; + } +};