You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3 KiB
Java

import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// read values with in.next...() methods
int iterations = in.nextInt();
// code your solution here
String sequence = getSequence(iterations, new StringBuilder("1"));
// Write result with System.out.println()
System.out.println(sequence);
}
private static String getSequence(int input, StringBuilder previousSequence) {
if (input == 0) {
return previousSequence.toString();
}
int counter = 0;
StringBuilder sequence = new StringBuilder();
char currentCharacter = previousSequence.charAt(0);
for (int i = 0; i < previousSequence.length(); i++) {
char character = previousSequence.charAt(i);
if (character != currentCharacter) {
sequence.append(counter) ; //first add the count
sequence.append(currentCharacter); //count follwed by the actual number
counter = 0;
currentCharacter = character;
}
counter++;
}
sequence.append(counter) ; //first add the count
sequence.append(currentCharacter); //count follwed by the actual number
return getSequence(input - 1, sequence);
}
}