## The challenge

In this challenge, we are going to reverse a string while maintaining the spaces (if any) in their original place.

For example:

 ``````1 2 3 4 5 6 `````` ``````solve("our code") = "edo cruo" -- Normal reversal without spaces is "edocruo". -- However, there is a space at index 3, so the string becomes "edo cruo" solve("your code rocks") = "skco redo cruoy". solve("andrew") = "werdna" ``````

More examples in the test cases. All input will be lower case letters and in some cases spaces.

## The solution in Java code

Option 1:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 `````` ``````class Solution { public static String solve(String s) { //string to return String reversed = ""; //removes all whitespace String noSpace = s.replaceAll(" ", ""); //remembers index of which character to use int noSpaceCount = noSpace.length()-1; //for each character in s for(int i = 0;i

Option 2:

 `````` 1 2 3 4 5 6 7 8 9 10 `````` ``````import java.util.stream.IntStream; class Solution { public static String solve(String s) { StringBuilder str = new StringBuilder(s.replaceAll(" ", "")).reverse(); IntStream.range(0, s.length()) .filter(i -> s.charAt(i) == ' ') .forEach(j -> str.insert(j, ' ')); return str.toString(); } } ``````

Option 3:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 `````` ``````import java.util.ArrayList; class Solution{ public static String solve(String s) { StringBuilder sb = new StringBuilder(s).reverse(); for(int i = 0; i < s.length(); i++) { if(sb.charAt(i) == ' ') sb.deleteCharAt(i); if(s.charAt(i) == ' ') sb.insert(i, " "); } return sb.toString(); } } ``````

## Test cases to validate our solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 `````` ``````import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; import java.util.Random; public class SolutionTest{ private static Random random = new Random(); public static String abc2(String s) { String st = ""; String noSpace = s.replaceAll("\\s+",""); int j = noSpace.length()-1; for (int i = 0; i < s.length(); ++i) { if (s.charAt(i) == ' ') st += " "; else { st += noSpace.charAt(j); j--; } } return st; } private static String rand1() { String s = "abcdefghijklmnopqrstuvwxyzab"; String st = ""; boolean flag = false; int c = 0; int len = random(10,200); for (int i = 0; i < len; ++i) { if (i > 0 && i < len-2 && flag == true) { int t = random(0,10); if (t < 3) st += " "; flag = false; } else { int x = random(0,26); st += s.charAt(x); c++; if (c > 1) { flag = true; c = 0; } } } return st; } private static int random(int l, int u) { return random.nextInt(u-l)+l; } @Test public void basicTests() { assertEquals("srawedoc",Solution.solve("codewars")); assertEquals("edoc ruoy",Solution.solve("your code")); assertEquals("skco redo cruoy",Solution.solve("your code rocks")); } @Test public void randomTests() { for(int i=0;i<100;i++) { String st = rand1(); System.out.println(st); assertEquals(abc2(st),Solution.solve(st)); } } } ``````