javascript simple solution


  • 1
    M

    match the a + bi pattern, so there must be only 1 + sign. split and then parse numbers.

    javascript solution

    const matchVars = (str) => str.slice(0, -1).split('+').map(n => Number.parseInt(n, 10))
    
    /**
     * @param {string} a
     * @param {string} b
     * @return {string}
     */
    const complexNumberMultiply = function(a, b) {
      const [arl, aim] = matchVars(a)
      const [brl, bim] = matchVars(b)
    
      const [r, i] = [arl * brl + aim * bim * -1, arl * bim + aim * brl]
      return `${r}+${i}i`
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.