-2

example if i have an input like:

Heeeeeeeeeeeellooooo

the output have to be:

H9e3e2l5o

This is the code that I wrote so far:

    public class RLE {
        public static String encode(String s) {
            if (s == "" || s == null) return "";
            StringBuilder sb = new StringBuilder();
            int count = 1;
            char previous = s.charAt(0);
            char current;

            for (int i = 1; i < s.length(); i++) {
                current = s.charAt(i);
                if (current == previous) {
                    count++;
                } else {
                    if (count == 1) {
                        sb.append(previous);
                    } else if (count > 1) {
                        sb.append(count).append(previous);
                        count = 1;
                    }
                }
                previous = current;

            }

            return sb.toString();
        }

Results in:

'Heeeeeeeeeeeellooooo' -> H12e2l

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 4
    What is your question? – Mark Rotteveel Apr 20 '17 at 09:00
  • 3
    I understand that your code doesn't deliver the expected output. But what I don't understand - what is the rule that says that your Hello should result in H9e3e ... (your first example is break up 12 into 9+3 ... but ... why? Why 9+3? – GhostCat Apr 20 '17 at 09:04

3 Answers3

2

This will work for you

public static String encode(String s) {
    if (s == "" || s == null)
        return "";
    StringBuilder sb = new StringBuilder();
    int count = 1;
    char previous = s.charAt(0);
    char current;

    for (int i = 1; i < s.length(); i++) {
        current = s.charAt(i);
        if (current == previous) {
            count++;
        } else {
            if (count == 1) {
                sb.append(previous);
            } else if (count > 1) {
                if (count > 9) {
                    sb.append(9).append(previous);
                    sb.append(count - 9).append(previous);
                } else {
                    sb.append(count).append(previous);
                }
                count = 1;
            }
        }
        previous = current;

    }
    sb.append(count).append(previous);
    return sb.toString();
}
0

The code is missing

if (count > 1) {
  sb.append(count);
}
sb.append(previous)

after for loop.

0

You are missing the case of last char of the string equals previous char then that is not printing. Add increment logic in if(current == previous) case also

                    if (current == previous) {
                    count++;
                    if(i == s.length()-1){
                        if (count == 1) {
                            sb.append(previous);
                        } else if (count > 1) {
                            sb.append(count).append(previous);
                            count = 1;
                        }
                    }
                }
Prashanth Debbadwar
  • 1,047
  • 18
  • 33