How to compress a string using recursion? (RLE algorithm)

I have small problems trying to compress a string using recursion.

For example, consider the following line:

qwwwwwwwwweeeeerrtyyyyyqqqqqwEErTTT

After applying the RLE algorithm, this string is converted to:

q9w5e2rt5y4qw2Er3T

In the compressed line, “9w” represents a sequence of nine consecutive lowercase “w” characters. "5e" is 5 consecutive lowercase e characters, etc.

I already have code for compression without recursion:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Compress {

    public static String input(String source) {
        StringBuffer coding = 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++;

           }
            if (runLength>1){
            coding.append(runLength);
            }
            coding.append(source.charAt(i));
        }
        return coding.toString();
    }

    public static void main(String[] args) {

        IO.outputStringAnswer("Enter a string");
        String str = IO.readString();
        String result = ""; 
        result=input(str); //function(variable)


        IO.outputStringAnswer(result);
    }
}

But I'm not sure if this can be turned into a recursive version of this.

+4
source share
2

, , , :

public static String compress(String source) {
    if (source.length() <= 1) return source;

    int runLength = 1;
    while (runLength < source.length() && source.charAt(0) == source.charAt(runLength)) {
        runLength++;
    }

    String lengthString = runLength > 1 ? String.valueOf(runLength) : "";
    return lengthString + source.substring(0,1) + compress(source.substring(runLength));
}

, String . , .

+4

. , , :

public static String compressRecursion(String str, char curChar, int curCount) {
    // termination case - reached end of the source string
    if(str.length() == 0)
        return "" + (curCount == 1 ? "" : curCount) + curChar;

    // branch on change in next character
    String nextStr = str.substring(1,str.length());
    if(str.charAt(0) == curChar)
        return compressRecursion(nextStr,curChar,curCount + 1);
    else
        return "" + (curCount == 1 ? "" : curCount) + curChar
                + compressRecursion(nextStr,str.charAt(0),1);
}
public static String compress(String source) {
    return compressRecursion(source, source.charAt(0), 0);
}

, , "Stack Overflow" , , , , . Java .

, ( ):

(define (num2str num)
  (if (= 1 num) "" (number->string num)))

(define (first-char str)
  (substring str 0 1))

(define (next-string str)
  (substring str 1 (string-length str)))

(define (compress str char count)
  (cond [(= 0 (string-length str)) (string-append (num2str count) char)]
        [(string=? char (first-char str))
          (compress (next-string str) char (+ count 1))]
        [ else 
          (string-append (num2str count) char
            (compress (next-string str) (first-char str) 1))]))

(define (compressStart str)
  (compress str (first-char str) 0)) 

, , , , Java.

+2

All Articles