Sha256: b78f2532f6db1f6cbd192e32026c9f13e70e3d8f6b8df106513110636abdeaac

Contents?: true

Size: 1.85 KB

Versions: 122

Compression:

Stored size: 1.85 KB

Contents

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

public class RunLengthEncoding {
    public String encode(String data) {

        StringBuilder encodedDataBuilder = new StringBuilder();

        if (data.length() == 0) {
            return data;
        }

        int previousCharCount = 1;
        char previousChar = data.charAt(0);

        for (int i = 1; i < data.length(); i++) {
            char currentChar = data.charAt(i);

            if (previousChar == currentChar) {
                previousCharCount++;
            }

            if (previousChar != currentChar || i == data.length() - 1) {
                addChars(encodedDataBuilder, previousChar, previousCharCount);

                if (previousChar != currentChar && i == data.length() - 1) {
                    addChars(encodedDataBuilder, currentChar, 1);
                }

                previousChar = currentChar;
                previousCharCount = 1;
            }
        }

        return encodedDataBuilder.toString();
    }

    public void addChars(StringBuilder toAddTo, char toAdd, int countOfChar) {
        if(countOfChar != 1) {
            toAddTo.append(countOfChar);
        }

        toAddTo.append(toAdd);
    }

    public String decode(String encodedData) {
        StringBuilder decodedData = new StringBuilder();
        Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z\\s]");
        Matcher matcher = pattern.matcher(encodedData);

        while (matcher.find()) {
            try {
                int number = Integer.parseInt(matcher.group());

                matcher.find();

                for (int i = 0; i<number; i++) {
                    decodedData.append(matcher.group());
                }
            } catch (NumberFormatException e) {
                decodedData.append(matcher.group());
            }
        }

        return decodedData.toString();
    }
}

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
trackler-2.2.1.71 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.70 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.69 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.68 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.67 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.66 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.65 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.64 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.63 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.62 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.61 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.60 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.59 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.58 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.57 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.56 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.55 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.54 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.53 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java
trackler-2.2.1.52 tracks/java/exercises/run-length-encoding/.meta/src/reference/java/RunLengthEncoding.java