Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Multiply two Strings

/*
	This is an implementation that demonstrates
	how to efficiently multiply two int numbers
	that are represented as strings.

	Let M be the number of digits in first
	and N the number of digits in second.

	Time complexity: O(M*N) 
	Space complexity: O(M+N)
*/

public class MultiplyStrings {
	public static void main(String[] args) {
		String num1 = "123", num2 = "456";
		System.out.println(multiply(num1, num2)); // 56088
	}
	private static String multiply(String num1, String num2) {
		// If either is 0, return 0
		if (num1.equals("0") || num2.equals("0")) {
			return "0";
		}
		// Convert the numbers into StringBuilder objects
		StringBuilder firstNum = new StringBuilder(num1);
		StringBuilder secondNum = new StringBuilder(num2);

		// Reverse both numbers before performing the multiplication
		firstNum.reverse();
		secondNum.reverse();

		// Create answer variable as placeholder for
		// multiplication of each digit of firstNum with secondNum
		int N = firstNum.length() + secondNum.length();
		StringBuilder answer = new StringBuilder();
		for (int i = 0; i < N; i++) {
			answer.append("0");
		}
		int digit1, digit2;
		for (int place2 = 0; place2 < secondNum.length(); place2++) {
			digit2 = secondNum.charAt(place2) - '0';
			// Multiply current digit2 by all digits in firstNum
			for (int place1 = 0; place1 < firstNum.length(); place1++) {
				digit1 = firstNum.charAt(place1) - '0';
				// Current position in answer depends on place
				// of digit2 and place of digit1
				int currentPos = place1 + place2;
				// Digit currently at position currentPos is carried over
				// and summed with current result
				int carry = answer.charAt(currentPos) - '0';
				int product = digit1 * digit2 + carry;
				// Set the ones place of product
				answer.setCharAt(currentPos, (char) (product % 10 + '0'));
				// Add the carry to next position
				int value = (answer.charAt(currentPos + 1) - '0') +
						product / 10;
				answer.setCharAt(currentPos + 1, (char) (value + '0'));
			}
		}
		// Remove excess 0 from the rear of the answer
		if (answer.charAt(answer.length() - 1) == '0') {
			answer.deleteCharAt(answer.length() - 1);
		}
		answer.reverse();
		return answer.toString();
	}

}
Comment

multiplying a string

2*'string'
Comment

Multiply Strings

class Solution {
public:
    string multiply(string num1, string num2) {
        
    }
};
Comment

Multiply Strings

class Solution {
    public String multiply(String num1, String num2) {
        
    }
}
Comment

Multiply Strings



char * multiply(char * num1, char * num2){

}
Comment

Multiply Strings

public class Solution {
    public string Multiply(string num1, string num2) {
        
    }
}
Comment

Multiply Strings

/**
 * @param {string} num1
 * @param {string} num2
 * @return {string}
 */
var multiply = function(num1, num2) {
    
};
Comment

Multiply Strings

# @param {String} num1
# @param {String} num2
# @return {String}
def multiply(num1, num2)
    
end
Comment

Multiply Strings

class Solution {
    func multiply(_ num1: String, _ num2: String) -> String {
        
    }
}
Comment

Multiply Strings

class Solution {

    /**
     * @param String $num1
     * @param String $num2
     * @return String
     */
    function multiply($num1, $num2) {
        
    }
}
Comment

Multiply Strings

function multiply(num1: string, num2: string): string {

};
Comment

PREVIOUS NEXT
Code Example
Java :: javafx detect collision 
Java :: Java @Inherited 
Java :: get device id programmatically android 
Java :: java coding standards variables 
Java :: java days between 
Java :: how to make a char uppercase in java 
Java :: dupplicate classes android 
Java :: java int to hex 
Java :: java array loop backwards 
Java :: java how to override a private method 
Java :: app dashboard cardview click effect and animation android studio 
Java :: even digit sum java 
Java :: java 8 group a collections by 2 property 
Java :: procedural programming Java code example 
Java :: java string to path 
Java :: slice array java 
Java :: offsetdattime to date 
Java :: java sout two dimensional array 
Java :: java filedialog 
Java :: replace all punctuation in string java 
Java :: Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema 
Java :: java try catch 
Java :: java obtain list string from list object 
Java :: java launch exe 
Java :: android activity read intent extra 
Java :: cosinus-1 java 
Java :: delete ending part of the string java 
Java :: pass list to intent in android java 
Java :: java split string 
Java :: java how to cast int to String 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =