Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looks like <code>PAYPALISTHEFASTERSAFERWAYTOSENDMONEY</code> is the input and </p> <pre><code>P A Y P A L F E R W A I A M O N Y S S D Y E T T R N E S O H E T S A F E </code></pre> <p>is the output to me..</p> <p>Even though the question was not clearly stating to provide an algorithm initially. Here is pseudo code for the recursive solution:</p> <pre><code>convert(input): spiral(out[][],input,0,0,sqrt(input.len)) return out.toString() spiral(out[][],input,ix,iy,size) if size&gt;0 //calculate the frame coords with starting indices ix,iy &amp; size of the frame place first 4*(size-1) chars on a frame on the ´out´ matrix //recursive call to create inner frame spiral(out,input.remainingString(),ix+1,iy+1,size-2) else return </code></pre> <p>and implementation in java:</p> <pre><code>public class PayPal { private enum Dir { RIGHT, DOWN, LEFT, UP; } public String convert(String input) { double dRoot = Math.sqrt(input.length()); int root; if (Double.compare(dRoot, (int) dRoot) == 0) { root = (int) dRoot; } else { root = (int) dRoot + 1; } char[][] out = new char[root][root]; spiral(out, 0, 0, root, input); StringBuilder sb = new StringBuilder(); for (char[] line : out) { sb.append(line); } return sb.toString(); } private void spiral(char[][] out, int i, int j, int size, String input) { Dir direction = Dir.RIGHT; if (size &gt; 0) { if (size == 1) { out[i][j] = input.charAt(0); } else { for (int k = 0; k &lt; 4 * (size - 1); k++) { int di = (k != 0 &amp;&amp; k % (size - 1) == 0 ? size - 1 : k % (size - 1)); switch (direction) { case RIGHT: out[i][j + di] = input.charAt(k); break; case DOWN: out[i + di][j + size - 1] = input.charAt(k); break; case LEFT: out[i + size - 1][j + size - 1 - di] = input.charAt(k); break; case UP: out[i + size - 1 - di][j] = input.charAt(k); break; } if (k != 0 &amp;&amp; (k % (size - 1) == 0)) //Change direction { direction = Dir.values()[direction.ordinal() + 1]; } } } spiral(out, i + 1, j + 1, size - 2, input.substring(4 * (size - 1))); } else { return; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload