Simple string reversal in Java

The challenge

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

For example:

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"
Code language: JavaScript (javascript)

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:

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<s.length();i++) { if(s.charAt(i)==' ') { //if it's a space, add a space reversed +=" "; } else { //if not, then add one of characters from the end of the string reversed += String.valueOf(noSpace.charAt(noSpaceCount)); //decrements to next character to add noSpaceCount --; } } return reversed; } }
Code language: Java (java)

Option 2:

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(); } }
Code language: Java (java)

Option 3:

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(); } }
Code language: Java (java)

Test cases to validate our solution

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)); } } }
Code language: Java (java)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments