0
public static String decompressString (String text) {
    int count = 0;
    StringBuilder result = new StringBuilder () ;
    for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        if (Character.isDigit(c)) {
            count = count * 10 + c - '0';
        } else { 
            while (count >0){ 
                result.append(c);
                count--;
            }
        }

    }
    return result.toString();
}

The program is supposed to take in a run length encoded string from the main method such as 5A5Bcd and return the string in a run length decoded format. 5A5Bcd -> AAAAABBBBBcd. The problem i am running into is that the code seems to ignore characters that are not preceeded by a digit. In the example above i am returning AAAAABBBBB instead of AAAAABBBBBcd; the 'c' and 'd' are not preceeded by a digit and therefore arent recognized. Any ideas, I have been stuck at this point for quite some time now.

cise
  • 69
  • 1
  • 8

4 Answers4

2

Your count variable is not going to be nonzero when you encounter the "c" and "d" characters in your example, because it will have been decremented to zero after processing the "5B".

The simplest fix I see in your code is to add a check before the while loop:

if (Character.isDigit(c)) {
    // ...
} else {
    if (count == 0) {
        // Single-run-length characters have an implicit "1" prepended
        count = 1;
    }
    while (count > 0) {
        // ..
    }
}
Ian W.
  • 36
  • 3
0

Whenever you start working on a new character, count is at 0, therefore nothing will be appended. You want count to be 1 at the beginning of the loop and set it to 1 after the while(count > 0) loop. Can you explain why you do count = count * 10 + c - '0'; instead of count = c (That will have to be changed as well)?

T. Schalke
  • 36
  • 1
  • The count = count * 10 + c - '0' accounts for digits that are more than one digit such as 10, 100 , 1000 etc. Ian and you have both helped me and the code is working as wanted now. Thank you both. – cise Mar 04 '18 at 23:13
0

You can solve this problem as below

private static String decode(String encodedString) {
        String decodedString = null;
        //aaabbbcccccdd
        //3a3b5c2d
        
        int n = encodedString.length();
        StringBuilder sb= new StringBuilder();
        for (int i = 0; i < n; i++) {
            if(i+1 <n && i%2 ==0)
            sb.append(repeat(Integer.parseInt(String.valueOf(encodedString.charAt(i))),encodedString.charAt(i+1)));
        }
        
        return sb.toString();
                
    }

    private static String repeat(int length, char charAt) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < length; j++) {
            sb.append(charAt);
        }
        return sb.toString();
    }
0
public class RunLengthDecode {
    public static void main(String[] args) {
        
        String string="9a8b8c5d";
        String resultString="";
        

        for (int i = 0; i < string.length(); i+=2) {
            int count=Character.getNumericValue(string.charAt(i));
            
            for(int j=0;j<count;j++) {
            resultString+=string.charAt(i+1);
            }
        }
        System.out.println(resultString);
        
    }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 1
    Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Suraj Rao Mar 31 '22 at 06:33
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 31 '22 at 07:07