Ad
Strings
Arrays

Final solution, no given as argument a void string.

Code
Diff
  • import java.text.ParseException;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    
    public class Kata{
    	public static String[] replaceLetter(String str, int n) {
    
    		ArrayList<String> arr = new ArrayList<>();
    		StringBuilder strFinal = new StringBuilder(str);
    		int nextIndex = n - 1;
    		int posSpaces = n;
    		
    		// Find first spaces
    		for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') {
    			posSpaces++;
    			nextIndex++;
    		}
    		
    		// Iterate string 
    		for (int i = 0; i < str.length(); i++) {
    			nextIndex = (nextIndex + 1) % str.length();
    
    			if (str.charAt(i) != ' ') {
    				while (str.charAt(nextIndex) == ' ')  {
    					nextIndex = (nextIndex + 1) % str.length();
    				}
    
    				// Replace letters
    				strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    				arr.add(strFinal.toString());
    				// Reset string to add
    				strFinal = new StringBuilder(str);
    			} else{ // current index is white space
    				nextIndex --;
    			}
    
    		}
    
    		return arr.toArray(new String[arr.size()]);
    
    	}
     }
    • import java.text.ParseException;
    • import java.util.ArrayList;
    • import java.util.Arrays;
    • import java.util.Random;
    • import java.util.concurrent.ThreadLocalRandom;
    • public class Kata{
    • public static String[] replaceLetter(String str, int n) {
    • public static String[] replaceLetter(String str, int n) {
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • for (int i = 0; i < str.length(); i++) {
    • int nextIndex = (i + n) % str.length();
    • if (str.charAt(i) != ' ') {
    • if (str.charAt(nextIndex) != ' ') {
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • } else {
    • while (str.charAt(nextIndex) == ' ') {
    • if (nextIndex == str.length() - 1) {
    • nextIndex = 0;
    • } else {
    • nextIndex++;
    • }
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • int nextIndex = n - 1;
    • int posSpaces = n;
    • // Find first spaces
    • for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') {
    • posSpaces++;
    • nextIndex++;
    • }
    • // Iterate string
    • for (int i = 0; i < str.length(); i++) {
    • nextIndex = (nextIndex + 1) % str.length();
    • if (str.charAt(i) != ' ') {
    • while (str.charAt(nextIndex) == ' ') {
    • nextIndex = (nextIndex + 1) % str.length();
    • }
    • // Replace letters
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • // Reset string to add
    • strFinal = new StringBuilder(str);
    • } else{ // current index is white space
    • nextIndex --;
    • }
    • }
    • }
    • }
    • return arr.toArray(new String[arr.size()]);
    • return arr.toArray(new String[arr.size()]);
    • }
    • }
Strings
Arrays
Strings
Arrays
Code
Diff
  • import java.text.ParseException;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.concurrent.ThreadLocalRandom;
    
    public class Kata{
    	public static String[] replaceLetter(String str, int n) {
    		ArrayList<String> arr = new ArrayList<>();
    		StringBuilder strFinal = new StringBuilder(str);
    
    		for (int i = 0; i < str.length(); i++) {
    			int nextIndex = (i + n) % str.length();
    			if (str.charAt(i) != ' ') {
    
    				if (str.charAt(nextIndex) != ' ') {
    
    					strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    					arr.add(strFinal.toString());
    					strFinal.replace(0, str.length(), str);
    
    				} else {
    					while (str.charAt(nextIndex) == ' ') {
    
    						if (nextIndex == str.length() - 1) {
    							nextIndex = 0;
    						} else {
    							nextIndex++;
    						}
    					}
    					strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    					arr.add(strFinal.toString());
    					strFinal.replace(0, str.length(), str);
    
    				}
    			}
    
    		}
    
    		return arr.toArray(new String[arr.size()]);
    
    	}
     }
    • import java.text.ParseException;
    • import java.util.ArrayList;
    • import java.util.Arrays;
    • import java.util.Random;
    • import java.util.concurrent.ThreadLocalRandom;
    • public class Kata{
    • public static String[] replaceLetter(String str, int n) {
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • for (int i = 0; i < str.length(); i++) {
    • int nextIndex = (i + n) % str.length();
    • int nextIndex = (i + n) % str.length();
    • if (str.charAt(i) != ' ') {
    • if (str.charAt(nextIndex) != ' ') {
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • } else {
    • while (str.charAt(nextIndex) == ' ') {
    • nextIndex++;
    • if (nextIndex == str.length() - 1) {
    • nextIndex = 0;
    • } else {
    • nextIndex++;
    • }
    • }
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • }
    • }
    • }
    • return arr.toArray(new String[arr.size()]);
    • }
    • }
Strings
Arrays

Arreglado el problema de los espacios DEFINITIVAMENTE, añadido test

Code
Diff
  • import java.text.ParseException;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.concurrent.ThreadLocalRandom;
    
    public class Kata{
    	public static String[] replaceLetter(String str, int n) {
    
    		ArrayList<String> arr = new ArrayList<>();
    		StringBuilder strFinal = new StringBuilder(str);
    
    		for (int i = 0; i < str.length(); i++) {
    			int nextIndex = (i + n) % str.length();
    			if (str.charAt(i) != ' ') {
    
    				if (str.charAt(nextIndex) != ' ') {
    
    					strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    					arr.add(strFinal.toString());
    					strFinal.replace(0, str.length(), str);
    
    				} else {
    					while (str.charAt(nextIndex) == ' ') {
    						nextIndex++;
    					}
    					strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    					arr.add(strFinal.toString());
    					strFinal.replace(0, str.length(), str);
    
    				}
    			}
    
    		}
    
    		return arr.toArray(new String[arr.size()]);
    
    	}
     }
    • import java.text.ParseException;
    • import java.util.ArrayList;
    • import java.util.Arrays;
    • import java.util.Random;
    • import java.util.concurrent.ThreadLocalRandom;
    • public class Kata{
    • public static String[] replaceLetter(String str, int n) {
    • public static String[] replaceLetter(String str, int n) {
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • for (int i = 0; i < str.length(); i++) {
    • int nextIndex = (i + n) % str.length() ;
    • int nextIndex = (i + n) % str.length();
    • if (str.charAt(i) != ' ') {
    • if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
    • if (str.charAt(nextIndex) != ' ') {
    • nextIndex = (i + n) % str.length();
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • } else {
    • while (str.charAt(nextIndex) == ' ') {
    • nextIndex++;
    • }
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • }
    • }
    • }
    • return arr.toArray(new String[arr.size()]);
    • }
    • }
Strings

Last Tests add

Code
Diff
  • import java.text.ParseException;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.concurrent.ThreadLocalRandom;
    
    public class Kata{
    public static String[] replaceLetter(String str, int n) {
    
    		ArrayList<String> arr = new ArrayList<>();
    		StringBuilder strFinal = new StringBuilder(str);
    
    		for (int i = 0; i < str.length(); i++) {
    			int nextIndex = (i + n) % str.length() ;
    
    			if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
    
    				nextIndex = (i + n) % str.length();
    				strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    				arr.add(strFinal.toString());
    				strFinal.replace(0, str.length(), str);
    			}
    
    		}
    
    		String[] array = arr.toArray(new String[arr.size()]);
    
    		return array;
    
    	}
     }
    • import java.text.ParseException;
    • import java.util.ArrayList;
    • import java.util.Arrays;
    • import java.util.Random;
    • import java.util.concurrent.ThreadLocalRandom;
    • public class Kata{
    • public static String[] replaceLetter(String str, int n) {
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • for (int i = 0; i < str.length(); i++) {
    • int nextIndex = (i + n) % str.length() ;
    • if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
    • nextIndex = (i + n) % str.length();
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • }
    • }
    • System.out.println(arr.toString());
    • String[] array = arr.toArray(new String[arr.size()]);
    • return array;
    • }
    • }
Strings

Cambio al inicializar el nextIndex

Code
Diff
  • import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.concurrent.ThreadLocalRandom;
    
    public class Kata{
    public static String[] replaceLetter(String str, int n) {
    
    		ArrayList<String> arr = new ArrayList<>();
    		StringBuilder strFinal = new StringBuilder(str);
    
    		for (int i = 0; i < str.length(); i++) {
    			int nextIndex = (i + n) % str.length();
    
    			if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
    
    				nextIndex = (i + n) % str.length();
    				strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    				arr.add(strFinal.toString());
    				strFinal.replace(0, str.length(), str);
    			}
    
    		}
    
    		System.out.println(arr.toString());
    
    		String[] array = arr.toArray(new String[arr.size()]);
    
    		return array;
    
    	}
     }
    • import java.text.ParseException;
    • import java.util.ArrayList;
    • import java.util.Arrays;
    • import java.util.Random;
    • import java.util.concurrent.ThreadLocalRandom;
    • public class Kata{
    • public static String[] replaceLetter(String str, int n) {
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • for (int i = 0; i < str.length(); i++) {
    • int nextIndex = 0;
    • int nextIndex = (i + n) % str.length();
    • if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
    • nextIndex = (i + n) % str.length();
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • }
    • }
    • System.out.println(arr.toString());
    • String[] array = arr.toArray(new String[arr.size()]);
    • return array;
    • }
    • }
Strings

Solución espacions en blanco

Code
Diff
  • import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.concurrent.ThreadLocalRandom;
    
    public class Kata{
    	public static String[] replaceLetter(String str) {
    
    		ArrayList<String> arr = new ArrayList<>();
    		StringBuilder strFinal = new StringBuilder(str);
    
    		for (int i = 0; i < str.length(); i++) {
    			int nextIndex = (i + 1) % str.length();
    
    			// check white space
    			if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
    
    				nextIndex = (i + 1) % str.length();
    				strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    				arr.add(strFinal.toString());
    				strFinal.replace(0, str.length(), str);
    			}
    
    		}
    
    		System.out.println(arr.toString());
    
    		String[] array = arr.toArray(new String[arr.size()]);
    
    		return array;
    
    	}
     }
    • import java.text.ParseException;
    • import java.util.ArrayList;
    • import java.util.Arrays;
    • import java.util.Random;
    • import java.util.concurrent.ThreadLocalRandom;
    • public class Kata{
    • public static String[] replaceLetter(String str) {
    • class Kata {
    • public static String[] replaceLetter(String str) {
    • ArrayList<String> arr = new ArrayList<>();
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • for (int i = 0; i < str.length(); i++) {
    • int nextIndex = (i + 1) % str.length();
    • if (str.charAt(i) != ' ') {
    • //comprobar si i + 1 es un espacio
    • int nextIndex = (i + 1) % str.length();
    • // check white space
    • if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
    • nextIndex = (i + 1) % str.length();
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • }
    • }
    • // Convertimos ArrayList a Array
    • String[] array = arr.toArray(new String[arr.size()]);
    • return array;
    • }
    • }
    • System.out.println(arr.toString());
    • String[] array = arr.toArray(new String[arr.size()]);
    • return array;
    • }
    • }
Strings

Prueba de descripción

Code
Diff
  • import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.concurrent.ThreadLocalRandom;
    
    
    class Kata {
      public static String[] replaceLetter(String str) {
    
    			// Hola -> oola, Hlla, Hoaa, HolH
          // Prueba añadir descripción
    			ArrayList<String> arr = new ArrayList<>();
    
    			StringBuilder strFinal = new StringBuilder(str);
    
    			for (int i = 0; i < str.length() - 1; i++) {
    
    				if (str.charAt(i) != ' ') {
    
    					strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1)));
    
    					arr.add(strFinal.toString());
    
    					strFinal.replace(0, str.length(), str);
    				}
    
    			}
    
    			strFinal.setCharAt(str.length() - 1, str.charAt(0));
    
    			arr.add(strFinal.toString());
    			
    			System.out.println(arr.toArray().toString());
    			
    			// Convertimos ArrayList a Array
    		    String[] array = arr.toArray(new String[arr.size()]);
    
    			return array;
    		}
    }
    • import java.text.ParseException;
    • import java.util.ArrayList;
    • import java.util.Arrays;
    • import java.util.Random;
    • import java.util.concurrent.ThreadLocalRandom;
    • class Kata {
    • public static String[] replaceLetter(String str) {
    • // Hola -> oola, Hlla, Hoaa, HolH
    • // Prueba añadir descripción
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • for (int i = 0; i < str.length() - 1; i++) {
    • if (str.charAt(i) != ' ') {
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • }
    • }
    • strFinal.setCharAt(str.length() - 1, str.charAt(0));
    • arr.add(strFinal.toString());
    • System.out.println(arr.toArray().toString());
    • // Convertimos ArrayList a Array
    • String[] array = arr.toArray(new String[arr.size()]);
    • return array;
    • }
    • }