Given two binary strings a
and b
, return their sum as a binary string.
Example 1:
Input: a = "11", b = "1" Output: "100"
Example 2:
Input: a = "1010", b = "1011" Output: "10101"
Constraints:
1 <= a.length, b.length <= 104
a
and b
consist only of '0'
or '1'
characters.program main
implicit none
character(len=:), allocatable :: a, b, c
a = '11'
b = '1'
call solve(a, b, c)
print *, c
a = '1010'
b = '1011'
call solve(a, b, c)
print *, c
contains
subroutine solve(a, b, c)
implicit none
character(len=*), intent(in) :: a, b
character(len=:), allocatable, intent(out) :: c
integer :: i, j, k, n
n = len(a)
allocate(character(len=n+1) :: c)
c = ''
do i = 1, n
j = i + 1
if (j > n) exit
k = iarithmetic_sum(a(i:j), b(i:j))
write (c(i:i), '(I1)') k
end do
end subroutine solve
function iarithmetic_sum(a, b) result(c)
implicit none
character(len=*), intent(in) :: a, b
integer :: c
integer :: i, n
n = len(a)
c = 0
do i = 1, n
c = c + ichar(a(i:i)) - ichar('0') + ichar(b(i:i)) - ichar('0')
end do
end function iarithmetic_sum
end program main
! Declare the input and output variables
character(len=*) :: a, b
character(len=1000) :: result
! Declare the variables to store the lengths of the strings
integer :: alen, blen
! Declare the variables to store the individual digits of the strings
integer :: adigit, bdigit
! Declare the variable to store the carry bit
integer :: carry
! Initialize the result string to all zeros
result = "0"
! Get the lengths of the input strings
alen = len_trim(a)
blen = len_trim(b)
! Initialize the carry bit to zero
carry = 0
! Loop through the digits of the input strings
do i = 1, max(alen, blen)
! Get the individual digits of the input strings
adigit = ichar(a(i:i)) - ichar("0")
bdigit = ichar(b(i:i)) - ichar("0")
! Add the digits and the carry bit
result(i:i) = char(mod(adigit + bdigit + carry, 2) + ichar("0"))
! Set the carry bit for the next iteration
carry = int(adigit + bdigit + carry >= 2)
end do
! If there is a carry bit left over, add it to the result string
if (carry == 1) then
result(i:i) = "1"
end if
! Output the result string
write (*,*) result
! End of the program
end
temp.f95:2:27: 2 | character(len=*) :: a, b | 1 Error: Entity with assumed character length at (1) must be a dummy argument or a PARAMETER temp.f95:2:30: 2 | character(len=*) :: a, b | 1 Error: Entity with assumed character length at (1) must be a dummy argument or a PARAMETER temp.f95:34:21: 34 | carry = int(adigit + bdigit + carry >= 2) | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type
def addBinary(a: str, b: str) -> str:
result, carry, i, j = "", 0, len(a) - 1, len(b) - 1
while i >= 0 or j >= 0 or carry:
if i >= 0:
carry += int(a[i])
i -= 1
if j >= 0:
carry += int(b[j])
j -= 1
result = str(carry % 2) + result
carry //= 2
return result
The algorithm initializes an empty result binary string, carry, and two pointers i and j pointing to the last element of strings a and b respectively. In each iteration: - If i is not out of bounds, add the corresponding binary number to the carry. - If j is not out of bounds, add the corresponding binary number to the carry. - Append the remainder of dividing carry by 2 to the result binary string (left side). - Update carry by dividing it by 2 (integer division).
This process continues until i and j pointers become out of bounds and there is no carry left.
Finally, return the result binary string.
std::string addBinary(std::string a, std::string b) {
std::string result = "";
int i = a.size() - 1, j = b.size() - 1, carry = 0;
while (i >= 0 || j >= 0 || carry) {
if (i >= 0) carry += a[i--] - '0';
if (j >= 0) carry += b[j--] - '0';
result = char(carry % 2 + '0') + result;
carry /= 2;
}
return result;
}
The algorithm initializes an empty result binary string, carry, and two pointers i and j pointing to the last element of strings a and b respectively. In each iteration: - If i is not out of bounds, add the corresponding binary number to the carry. - If j is not out of bounds, add the corresponding binary number to the carry. - Append the remainder of dividing carry by 2 to the result binary string (left side). - Update carry by dividing it by 2 (integer division).
This process continues until i and j pointers become out of bounds and there is no carry left.
Finally, return the result binary string.