Reputation: 37
Here's the basics of the assignment:
Your task is to write a program that decodes a sequence of characters which was encoded using a simple form of run-length encoding, as described by the rules below.
Any sequence of between 2 to 9 identical characters is encoded by two characters. The first character is the length of the sequence, represented by one of the characters 2 through 9. The second character is the value of the repeated character. A sequence of more than 9 identical characters is dealt with by first encoding 9 characters, then the remaining ones.
Any sequence of characters that does not contain consecutive repetitions of any characters is represented by a 1 character followed by the sequence of characters, terminated with another 1. If a 1 appears as part of the sequence, it is escaped with a 1, thus two 1 characters are output.
Example Input and Output:
Input:
9A1ABC131
1112 3124
111111
Output:
AAAAAAAAAABC111
12 344
11
My code that I have so far but doesn't quite work:
import java.util.Scanner;
public class RunLength {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String test = kb.nextLine();
System.out.println(decode(test));
}
public static String encode(String s) {
if (s == "" || s == null) return "";
StringBuffer sb = new StringBuffer();
int count = 0;
char ch = 0;
for (int i = 0; i < s.length(); i++) {
if (i == 0) {
ch = s.charAt(i);
count++;
} else {
if (ch == s.charAt(i)) {
count++;
} else {
sb.append(ch).append(count);
count = 1; // count is set to 1 as 1 occurrence of ch has been appended to the output
ch = s.charAt(i);
}
}
}
sb.append(ch).append(count);
return sb.toString();
}
public static String decode(String st) {
if (st == "" || st == null) return "";
char[] stArr = st.toCharArray();
char lastseen = 0;
StringBuffer sb = new StringBuffer();
for (char s : stArr) {
if (!Character.isDigit(s)) {
lastseen = s;
sb.append(s);
} else {
int n = Integer.parseInt(String.valueOf(s));
for (int i = 0; i < n - 1; i++) {
sb.append(lastseen);
}
}
}
return sb.toString();
}
public static int[] decode2(String source) {
int arrLength = 0;
for (int i = 0; i < source.length(); i += 2) {
int count = Character.getNumericValue(source.charAt(i));
arrLength += count;
}
int array[] = new int[arrLength];
int k = 0;
for (int i = 0; i < source.length(); i += 2) {
int count = Character.getNumericValue(source.charAt(i));
for (int j = 0; j < count; j++) {
array[i + k] = Character.getNumericValue(source.charAt(i + 1));
}
}
return array;
}
}
Upvotes: 1
Views: 2944
Reputation: 1676
I suggest you try to split your decode methods into two main logical parts: One for the case that you are actually performing a RLE and the other for the case where you have a sequence of non-repeated characters, in which 1s have to be escaped.
The latter is easy to implement and your solution for that part seems to be correct.
The former needs you to loop over the next characters until the end of the sequence has been found. If you read anything but a 1, just add it to the output. In case there is a 1, you need to look ahead at the next character to see whether there is another 1, meaning there is an unrepeated 1 that had to be escaped, which is to be added to the output (consuming the lookahead, too). If the look ahead is not a 1 (including the special case where there is no next character) the unrepeated characters sequence ends here and nothing is to be added to the output.
Since this page is not supposed to solve your homework, I won't post a complete solution, though.
Upvotes: 1