for solving this question we will take a smaller example first.
suppose, we have a cylinder of $radius = r$ and $height = h$. a string of $length = l$, when wound on its cylindrical surface, starting at point $A$ and ending at point $B$, gives $n=3$ turns.
we will assume $2$ more points $p_1$ and $p_2$ co-linear to $A$ and $B$. now, we will take single full turns of the cylinder $( Ap_1, p_1 p_{2}$ and $p_2B )$ to $n=3$ similar cylinders of $radius = r$ and $height = h$.
now, when we open the paper through which cylinder was formed (it is a rectangle) we will get the length of one turn of the string like in the image for all of the 3 cylinders.
now, when we join these $n=3$ rectangles we will get the whole length of string $l$ = length of diagonal of resultant rectangle.
$l = \sqrt{(3.2\pi r)^{2} + h^{2}}$
so for $n$ turns we get the formula,
$l = \sqrt{(n.2\pi r)^{2} + h^{2}}$ ..............................(1)
now, similar story for the cube,

we get the formula,
$l = \sqrt{(4a)^{2} + a^{2}}$ ............................(2)
where, $a$ is the length of side of the cube.
here, $a = n$. so, from equation (2),
$l = \sqrt{(4n)^{2} + n^{2}}$ .............................(3)
from equation (1) and (3),
$\sqrt{(n.2\pi r)^{2} + h^{2}} = \sqrt{(4n)^{2} + n^{2}}$
both sides are +ve. so, squaring on both sides,
$(n.2\pi r)^{2} + h^{2} = (4n)^{2} + n^{2}$
$4n^{2}\pi^{2}r^{2} + h^{2} = 16n^{2} + n^{2}$
$4n^{2}\pi^{2}r^{2} + h^{2} = 17n^{2}$
putting, $r = 2/\pi$
$4n^{2}\pi^{2}.(4/\pi^{2}) + h^{2} = 17n^{2}$
$16n^{2} + h^{2} = 17n^{2}$
$h^{2} = 17n^{2} - 16n^{2}$
$h^{2} = n^{2}$
both sides are +ve. so, taking square root of both sides.
$h = \pm n$
$h$ and $n$ are both +ve. so, eliminating the -ve possibility.
hence, $h = n$.