Skip to content

Commit

Permalink
MultiplyStrings
Browse files Browse the repository at this point in the history
  • Loading branch information
kalpak92 committed Apr 9, 2021
1 parent 16a70ae commit bf1c221
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ This repository contains the solutions to problems done from various resources f
## Linked List
- [x] [Add Two Numbers - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/AddTwoNumbers.java)
- [x] [Add Two Numbers, where numbers are given from MSB to LSB - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/AddTwoNumbersMSBToLSB.java)
- [x] [Multiply Strings - Leetcode]()
- [x] [Reverse a Linked List - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/ReverseLinkedList.java)
- [x] [Reverse a Linked List from Node m to Node n - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/ReverseSublist.java)
- [x] [Reverse Nodes in k group - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/ReverseLinkedListInKgroup.java)
Expand Down
70 changes: 70 additions & 0 deletions src/Leetcode/MultiplyStrings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package Leetcode;

/**
* @author kalpak
*
* Given two non-negative integers num1 and num2 represented as strings,
* return the product of num1 and num2, also represented as a string.
*
* Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
*
*
* Example 1:
* Input: num1 = "2", num2 = "3"
* Output: "6"
*
* Example 2:
* Input: num1 = "123", num2 = "456"
* Output: "56088"
*
*
* Constraints:
*
* 1 <= num1.length, num2.length <= 200
* num1 and num2 consist of digits only.
* Both num1 and num2 do not contain any leading zero, except the number 0 itself.
*
*/

public class MultiplyStrings {
public static String multiply(String num1, String num2) {
// Compute products from each pair of digits from num1 and num2
int n1 = num1.length();
int n2 = num2.length();

int[] products = new int[n1 + n2];

for (int i = n1 - 1; i >= 0; i--) {
for (int j = n2 - 1; j >= 0; j--) {
int d1 = num1.charAt(i) - '0';
int d2 = num2.charAt(j) - '0';

products[i + j + 1] += d1 * d2;
}
}

// carry each element over.
int carry = 0;

for (int i = products.length - 1; i >= 0; i--) {
int tmp = (products[i] + carry) % 10;
carry = (products[i] + carry) / 10;
products[i] = tmp;
}

// Output the result
StringBuilder result = new StringBuilder();
for(int i : products)
result.append(i);

while (result.length() != 0 && result.charAt(0) == '0')
result.deleteCharAt(0);

return result.length() == 0 ? "0" : result.toString();

}

public static void main(String[] args) {
System.out.println(multiply("123", "456"));
}
}

0 comments on commit bf1c221

Please sign in to comment.