Theoretically you can further increase the performance by initializing the StringBuilder to the required capacity.
public class Kata { public static String multiply(String input,int times){ var str = new StringBuilder(input.length() * times); for(int i = 0; i < times; i++) { str.append(input); } return str.toString(); } }
- public class Kata {
- public static String multiply(String input,int times){
String str = "";- var str = new StringBuilder(input.length() * times);
- for(int i = 0; i < times; i++) {
str += input;- str.append(input);
- }
return str;- return str.toString();
- }
- }
Java 11 made this boring ;]
public class Kata { public static String multiply(String input,int times){ return input.repeat(times); } }
public class Kata{public static String multiply(String input,int times){String save = input;for (int i = 1; i < times; i++) {System.out.println(input);input += save;}if(times == 0){input = "";}return input;- public class Kata {
- public static String multiply(String input,int times){
- return input.repeat(times);
- }
- }