Created
August 15, 2015 16:09
-
-
Save viveksyngh/6f00138b60e5ed961819 to your computer and use it in GitHub Desktop.
Given two numbers represented as strings, return multiplication of the numbers as a string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __author__ = 'Vivek' | |
| #Given two numbers represented as strings, return multiplication of the numbers as a string. | |
| # DO NOT USE BIG INTEGER LIBRARIES ( WHICH ARE AVAILABLE IN JAVA / PYTHON ). | |
| def multiply(A, B): | |
| res = "0" * (len(A) + len(B)) | |
| if A == '0' or B == '0' : | |
| return '0' | |
| elif A == '1' : | |
| return B | |
| elif B == '1' : | |
| return A | |
| count = 0 | |
| for i in range(len(A)-1, -1, -1) : | |
| if len(res)-count == 0 : | |
| temp = int(A[i]) * int(B) | |
| else : | |
| temp = int(res[:len(res)-count]) + int(A[i]) * int(B) | |
| res = str(temp) + res[len(res)-count:] | |
| count += 1 | |
| return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment