0

Run Length Encoding is taking a string with reoccurring characters, for example qqqqqWWWWr, and transforming it to 5q4Wr. The reoccurring character should be preceded with the amount of times it reoccurs consecutively. If it only occurs once in a row it should not be preceded by a number.

Here is what I have so far, and I cannot seem to get it to work properly:

public class Compress {

    public static void main(String[] args) {
        System.out.print("Enter a string: ");
        String s = IO.readString();

        int currentRunLength = 1;
        String compressedString = "";

        for (int i = 0; i < s.length(); i++) {
            if (i == s.length() - 1){
                if (currentRunLength == 1) {
                compressedString += s.charAt(i);
                break;
                } else {
                compressedString += currentRunLength + s.charAt(i);
                break;
                }
            }
            if (s.charAt(i) == s.charAt(i + 1)) {
                currentRunLength++;
            }
            if (s.charAt(i) != s.charAt(i + 1) && currentRunLength > 1) {
                compressedString += currentRunLength + s.charAt(i);
                currentRunLength = 1;
            }
            if (s.charAt(i) != s.charAt(i + 1) && currentRunLength == 1) {
                compressedString += s.charAt(i);
            }
        }
        IO.outputStringAnswer(compressedString);
    }
}

Example run

Enter a string: qqqqWWWr

RESULT: "117q90Wr"

3 Answers 3

1

Maybe you shouldn't reinvent the wheel when it's a decently-known algorithm. I found an implementation on Rosetta Code at the following URL: http://rosettacode.org/wiki/Run-length_encoding#Java

Re-pasting in case the original URL is removed:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RunLengthEncoding {
    public static String encode(String source) {
        StringBuffer dest = new StringBuffer();
        for (int i = 0; i < source.length(); i++) {
            int runLength = 1;
            while (i+1 < source.length() && source.charAt(i) == source.charAt(i+1)) {
                runLength++;
                i++;
            }
            dest.append(runLength);
            dest.append(source.charAt(i));
        }
        return dest.toString();
    }

    public static String decode(String source) {
        StringBuffer dest = new StringBuffer();
        Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z]");
        Matcher matcher = pattern.matcher(source);
        while (matcher.find()) {
            int number = Integer.parseInt(matcher.group());
            matcher.find();
            while (number-- != 0) {
                dest.append(matcher.group());
            }
        }
        return dest.toString();
    }

    public static void main(String[] args) {
        String example = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
        System.out.println(encode(example));
        System.out.println(decode("1W1B1W1B1W1B1W1B1W1B1W1B1W1B"));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is wrong algorithm. It doesn't take care of runLength overflow.
1

The first problem I see is on this line:

compressedString += currentRunLength + s.charAt(i);

You are adding an int to a char. The char is cast to int before addition, and then the result from the addition is appended to your String. The ASCII value of q is 113. 113 + 4 would explain the beginning of your String -> 117.

Comments

0

The problem is related to this line:

compressedString += currentRunLength + s.charAt(i)

What this is doing is taking int and adding it to char (which will result in an int), then adding it to the compressed string.

You can address that with this minor change, which will convert currentRunLength to a String, then append the char:

compressedString += String.valueOf(currentRunLength) + s.charAt(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.