Only if. If , the phases cancel in .
If. Put . The hypothesis gives , i.e. , for each generator , hence for every product of generators, which (up to phases) are all Pauli strings. The Pauli strings form a basis of the space of complex matrices, so commutes with every matrix. Taking the matrix units : applied to gives for all , so is diagonal with all diagonal entries equal, . Since is unitary, , so and .
Algorithm. Represent a signed Pauli string on qubits by bits and a sign bit. Starting from the identity tableau (row is , row is ), apply the gates of in order, conjugating every row: H, S and CNOT map each Pauli string to another one with a sign given by a simple Boolean formula, and only the columns of the one or two qubits the gate touches change, so each gate costs operations on the rows (Aaronson and Gottesman). Do the same for and compare the two tableaux, bits. The total time is .