← Home

537. Complex Number Multiplication

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
537

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 537. Complex Number Multiplication, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are complexNumberMultiply.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(1). Here splitting takes constant time as length of the string is very small (<20).
  • Space: O(1). Constant extra space is used.

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02Given two strings representing two complex numbers.
03
04You need to return a string representing their multiplication. Note i2 = -1 according to the definition.
05
06Example 1:
07Input: "1+1i", "1+1i"
08Output: "0+2i"
09Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
10Example 2:
11Input: "1+-1i", "1+-1i"
12Output: "0+-2i"
13Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
14Note:
15
16The input strings will not have extra blank.
17The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.
18**/
19
20//Your runtime beats 100.00 % of cpp submissions.
21class Solution {
22public:
23    string complexNumberMultiply(string a, string b) {
24        int ar = stoi(a.substr(0, a.find("+")));
25        int ai = stoi(a.substr(a.find("+")+1, a.size()));
26        int br = stoi(b.substr(0, b.find("+")));
27        int bi = stoi(b.substr(b.find("+")+1, b.size()));
28        
29        // cout << ar << " " << ai << " " << br << " " << bi << endl;
30        
31        int r = ar*br + ai*bi*-1;
32        int i = ar*bi + ai*br;
33        return to_string(r)+"+"+to_string(i)+"i";
34    }
35};
36
37/**
38Complexity Analysis
39
40Time complexity : O(1). Here splitting takes constant time as length of the string is very small (<20).
41
42Space complexity : O(1). Constant extra space is used.
43**/

Cost

Complexity

Time
O(1). Here splitting takes constant time as length of the string is very small (<20).
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(1). Constant extra space is used.
Auxiliary state plus the answer structure where the problem requires one.