tutorialcup
Add Binary Leetcode Solution
Problem Statement
Given two binary strings a and b, we have to add these two strings and then return the result as a binary string. Binary string are the strings that contains only 0s and 1s.
Example
a = "11", b = "1"
"100"
a = "1010", b = "1011"
"10101"
Approach
For adding two binary strings we have to perform addition bit by bit. As we know addition is performed from right end moving towards left bits. Therefore we have to reverse the given strings first and then we can perform addition of its bits starting from index 0.
For storing bit addition sequence we can create a string variable res and append the sum of two bits and carry at end of the res string for each bit position. Finally we will return the res string for output.
Algorithm
- Reverse the given strings.
- Create an empty string and a carry variable. Initialise carry with 0.
- Now iterate over the given string in a while loop and add bit of first and second string along with carry. Now according to value of addition append the resultant bit to res string and also update the carry for next bit .
- At last we have output string but it is in reverse order because we have performed addition on reversed input string for our convenience. Therefore reverse the output string and finally return it.
Implementation
C++ Program for Add Binary Leetcode Solution
#include
using namespace std;
string addBinary(string a, string b)
{
int carry=0;
string res="";
reverse(a.begin(),a.
www.tutorialcup.com/leetcode-solutions/add-binary-leetcod...
Add Binary Leetcode Solution
Problem Statement
Given two binary strings a and b, we have to add these two strings and then return the result as a binary string. Binary string are the strings that contains only 0s and 1s.
Example
a = "11", b = "1"
"100"
a = "1010", b = "1011"
"10101"
Approach
For adding two binary strings we have to perform addition bit by bit. As we know addition is performed from right end moving towards left bits. Therefore we have to reverse the given strings first and then we can perform addition of its bits starting from index 0.
For storing bit addition sequence we can create a string variable res and append the sum of two bits and carry at end of the res string for each bit position. Finally we will return the res string for output.
Algorithm
- Reverse the given strings.
- Create an empty string and a carry variable. Initialise carry with 0.
- Now iterate over the given string in a while loop and add bit of first and second string along with carry. Now according to value of addition append the resultant bit to res string and also update the carry for next bit .
- At last we have output string but it is in reverse order because we have performed addition on reversed input string for our convenience. Therefore reverse the output string and finally return it.
Implementation
C++ Program for Add Binary Leetcode Solution
#include
using namespace std;
string addBinary(string a, string b)
{
int carry=0;
string res="";
reverse(a.begin(),a.
www.tutorialcup.com/leetcode-solutions/add-binary-leetcod...