Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my response to this - The main issue I saw with your code is you creating Strings as temporary objects in multiple areas of the code - This makes it very inefficient as well as very slow. The other issue is that you want to externalize all you can from the loops. I have compiled it and run it and it works.</p> <pre><code>package com.rch.test; public class Transposer { public static String transpose(String s) { int length = s.length(); int end = length - 1; StringBuilder t = new StringBuilder(); for (int i = 0; i &lt; length / 2; i++) { t.append(s.charAt(i)); t.append(s.charAt(end)); end--; } // Length of String is odd if (length % 2 == 1) { // add character in middle of String to the end of the new String t.append(s.charAt(length / 2)); } return t.toString(); } public static String unTranspose(String s) { int length = s.length(); StringBuilder t = new StringBuilder(); if (length % 2 == 1) { for (int i = 0; i &lt; length; i += 2) { t.append(s.charAt(i)); } for (int i = length - 2; i &gt; 0; i -= 2) { t.append(s.charAt(i)); } } else if (length % 2 == 0) { for (int i = 0; i &lt; length - 1; i += 2) { t.append(s.charAt(i)); } for (int i = length - 1; i &gt; 0; i -= 2) { t.append(s.charAt(i)); } } return t.toString(); } public static void main(String[] args) { String testString = "bridge"; String transposedString = Transposer.transpose(testString); String finalString = Transposer.unTranspose(transposedString); System.out.println("1)" + testString); System.out.println("2)" + transposedString); System.out.println("3)" + finalString); } } </code></pre> <p>Output : 1)bridge 2)bergid 3)bridge</p>
    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.
    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