Ad
Strings
Arrays

Replace Letter

Given a string and a number (n), generate an array in which each position will be that same string but modifying the letter of its first position (0) by the one in its n position.

Input:

hello, 2

Output:

[lello, hollo, hehlo, heleo, helll]

 Restrictions:

  • Where n will always be a number between 0 and the length of the string.

  • It is case-insensitive and does not discriminate between letters, numbers, and special characters.

  • When current position + n is greater than the length of the string, it will return to start.

  • The spaces will be considered:

    • If there is a space, that position will not be replaced.

    • The space will not replace any letter.

      Keep in mind there may be more than one white space !!

For example

Given the string = "Hello world" and n=1

Should return:

["eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH"]

It would not admit:

Hellowword

Hell  word

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();
    • 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()]);
    • }
    • }
Strings
Arrays

Contemplado el caso de que al final de la palabra haya un espacio

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) {
    • public static String[] replaceLetter(String str, int n) {
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • 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) != ' ') {
    • for (int i = 0; i < str.length(); i++) {
    • int nextIndex = (i + n) % str.length();
    • if (str.charAt(i) != ' ') {
    • if (str.charAt(nextIndex) != ' ') {
    • if (str.charAt(nextIndex) != ' ') {
    • 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) == ' ') {
    • } 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);
    • }
    • }
    • 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()]);
    • }
    • return arr.toArray(new String[arr.size()]);
    • }
    • }
Strings
Arrays

Replace Letter

Given a string and a number (n), generate an array in which each position will be that same string but modifying the letter of its first position (0) by the one in its n position.

Input:

hello, 2

Output:

[lello, hollo, hehlo, heleo, helll]

 Restrictions:

  • where n will always be a number between 0 and the length of the string.

  • It is case-insensitive and does not discriminate between letters, numbers, and special characters.

  • When current position + n is greater than the length of the string, it will return to start.

  • The spaces will be considered:

    • If there is a space, that position will not be replaced.
    • The space will not replace any letter.

For example

Given the string = "hello world" and n=1

Should return:

[eello world, Hlllo world, Hello world, Heloo world, Hello oorld, Hello wrrld, Hello wolld, Hello wordd, Hello worlH]

It would not admit:

hellowword

hell  word

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);
    			}
    
    		}
    
    
    		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() ;
    • 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;
    • return arr.toArray(new String[arr.size()]);
    • }
    • }
Strings

RandomTest añadido

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 = (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

Parametro n añadido

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 = 0;
    
    			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) {
    • 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 + 1) % str.length();
    • int nextIndex = 0;
    • // check white space
    • if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
    • nextIndex = (i + 1) % str.length();
    • 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
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

			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;
		}
}