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.1.0.54 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.53 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.52 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.51 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.50 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.49 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.48 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.47 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.46 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.45 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.44 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.43 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.42 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.41 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.40 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.39 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.38 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.37 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.36 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java
trackler-2.1.0.34 tracks/java/exercises/run-length-encoding/src/example/java/RunLengthEncoding.java