The problem
On this problem, we’re going to reverse a string whereas sustaining the areas (if any) of their authentic place.
For instance:
remedy("our code") = "edo cruo"
-- Regular reversal with out areas is "edocruo".
-- Nevertheless, there's a area at index 3, so the string turns into "edo cruo"
remedy("your code rocks") = "skco redo cruoy".
remedy("andrew") = "werdna"
Extra examples within the check circumstances. All enter shall be decrease case letters and in some circumstances areas.
The answer in Java code
Choice 1:
class Answer {
public static String remedy(String s) {
//string to return
String reversed = "";
//removes all whitespace
String noSpace = s.replaceAll(" ", "");
//remembers index of which character to make use of
int noSpaceCount = noSpace.size()-1;
//for every character in s
for(int i = 0;i<s.size();i++) {
if(s.charAt(i)==' ') { //if it is a area, add an area
reversed +=" ";
} else {
//if not, then add considered one of characters from the top of the string
reversed += String.valueOf(noSpace.charAt(noSpaceCount));
//decrements to subsequent character so as to add
noSpaceCount --;
}
}
return reversed;
}
}
Choice 2:
import java.util.stream.IntStream;
class Answer {
public static String remedy(String s) {
StringBuilder str = new StringBuilder(s.replaceAll(" ", "")).reverse();
IntStream.vary(0, s.size())
.filter(i -> s.charAt(i) == ' ')
.forEach(j -> str.insert(j, ' '));
return str.toString();
}
}
Choice 3:
import java.util.ArrayList;
class Answer{
public static String remedy(String s) {
StringBuilder sb = new StringBuilder(s).reverse();
for(int i = 0; i < s.size(); i++) {
if(sb.charAt(i) == ' ') sb.deleteCharAt(i);
if(s.charAt(i) == ' ') sb.insert(i, " ");
}
return sb.toString();
}
}
Take a look at circumstances to validate our answer
import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.Random;
public class SolutionTest{
non-public static Random random = new Random();
public static String abc2(String s) {
String st = "";
String noSpace = s.replaceAll("s+","");
int j = noSpace.size()-1;
for (int i = 0; i < s.size(); ++i) {
if (s.charAt(i) == ' ') st += " ";
else {
st += noSpace.charAt(j);
j--;
}
}
return st;
}
non-public 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;
}
non-public static int random(int l, int u) {
return random.nextInt(u-l)+l;
}
@Take a look at
public void basicTests() {
assertEquals("srawedoc",Answer.remedy("codewars"));
assertEquals("edoc ruoy",Answer.remedy("your code"));
assertEquals("skco redo cruoy",Answer.remedy("your code rocks"));
}
@Take a look at
public void randomTests() {
for(int i=0;i<100;i++) {
String st = rand1();
System.out.println(st);
assertEquals(abc2(st),Answer.remedy(st));
}
}
}