The challenge
Write a function called repeat_str
which repeats the given string src
exactly count
times.
repeatStr(6, "I") // "IIIIII"
repeatStr(5, "Hello") // "HelloHelloHelloHelloHello"
The solution in Java
Java comes with a built-in
on the String
class. Called repeat
, which allows you to very easily repeat that string n
number of times.
"Hello".repeat(2); // "HelloHello"
Using String::repeat()
Therefore, we could solve our above problem as follows:
class Solution {
static String repeatStr(int repeat, String string) {
return string.repeat(repeat);
}
}
Obviously, we probably want to validate our input before we just do it, so maybe wrap it in a ternary:
public class Solution {
public static String repeatStr(final int repeat, final String string) {
return repeat >= 0 ? string.repeat(repeat) : "";
}
}
Write all the code yourself?
Alternatively, you could always just write the method yourself, doing something like this:
public class Solution {
public static String repeatStr(final int repeat, final String string) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < repeat; i++) {
sb.append(string);
}
return sb.toString();
}
}
Or maybe with less code, like this:
public class Solution {
public static String repeatStr(final int repeat, final String string) {
String s = "";
for (int i = 0; i < repeat; i++) s += string;
return s;
}
}
Java 8 and above
If you’re feeling Java8 and above thoughts, you could always do this:
public class Solution {
public static String repeatStr(final int repeat, final String string) {
return java.util.stream.IntStream.range(0, repeat).mapToObj(i -> string)
.collect(java.util.stream.Collectors.joining()).toString();
}
}
Or maybe combine it with the Collections
nCopies
possible solution:
import java.util.Collections;
public class Solution {
public static String repeatStr(final int repeat, final String string) {
return repeat < 0 ? "" : String.join("", Collections.nCopies(repeat, string));
}
}
Test cases to validate
It’s always important to have some tests which will help us validate our application code.
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.util.Random;
public class SolutionTest {
@Test public void test4a() {
assertEquals("aaaa", Solution.repeatStr(4, "a"));
}
@Test public void test3Hello() {
assertEquals("HelloHelloHello", Solution.repeatStr(3, "Hello"));
}
@Test public void test5empty() {
assertEquals("", Solution.repeatStr(5, ""));
}
@Test public void test0a() {
assertEquals("", Solution.repeatStr(0, "kata"));
}
private final char[] characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQERSTUVWXYZ0123456789!@#$%^&*()-=_+[]{}|;:,.<>/?`~".toCharArray();
@Test public void testRandom() {
final Random rand = new Random();
for (int testIteration = 0; testIteration < 10; ++testIteration) {
final StringBuilder text = new StringBuilder(rand.nextInt(32));
for (int i = 0; i < text.capacity(); ++i) {
final int chi = rand.nextInt(characters.length);
text.append(characters[chi]);
}
final String string = text.toString();
final int timesToRepeat = rand.nextInt(32);
final StringBuilder sb = new StringBuilder(timesToRepeat * string.length());
for (int i = 0; i < timesToRepeat; ++i) {
sb.append(text);
}
final String expected = sb.toString();
assertEquals(expected, Solution.repeatStr(timesToRepeat, string));
}
}
}