@Test public void runTest() { assertEquals("A11B4C6", Rle.rle(Mode.ENCODE, "AAAAAAAAAAABBBBCCCCCC")); assertEquals("AAAAAAAAAABBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC", Rle.rle(Mode.DECODE, "A10B4C30")); }
public static String rle(Mode mode, String input) { StringBuilder str = new StringBuilder(); if (mode == Mode.ENCODE) { int il = input.length(); for (int i = 0; i < il; i++) { int count = 1; while (i < il - 1 && input.charAt(i) == input.charAt(i + 1)) { count++; i++; } str.append(input.charAt(i)).append(count); } return str.toString(); } if (mode == Mode.DECODE) { char[] stArr = input.toCharArray(); char lastChar = 0; for (int p = 0; p < stArr.length; p++) { char s = stArr[p]; if (!Character.isDigit(s)) { lastChar = s; str.append(s); } else { int e = digitCount(stArr, p); int n = Integer.parseInt(String.valueOf(input.substring(p, (p + e)))); for (int i = 0; i < n - 1; i++) { str.append(lastChar); } } } } return str.toString(); }
Rle { public static String rle(Mode mode, String input) { StringBuilder str = new StringBuilder(); if (mode == Mode.ENCODE) { int il = input.length(); for (int i = 0; i < il; i++) { int count = 1; while (i < il - 1 && input.charAt(i) == input.charAt(i + 1)) { count++; i++; } str.append(input.charAt(i)).append(count); } return str.toString(); } if (mode == Mode.DECODE) { char[] stArr = input.toCharArray(); char lastChar = 0; for (int p = 0; p < stArr.length; p++) { char s = stArr[p]; if (!Character.isDigit(s)) { lastChar = s; str.append(s); } else { int e = digitCount(stArr, p); int n = Integer.parseInt(String.valueOf(input.substring(p, (p + e)))); for (int i = 0; i < n - 1; i++) { str.append(lastChar); } } } } return str.toString(); } }
Rle { public static String rle(Mode mode, String input) { StringBuilder str = new StringBuilder(); if (mode == Mode.ENCODE) { int il = input.length(); for (int i = 0; i < il; i++) { int count = 1; while (i < il - 1 && input.charAt(i) == input.charAt(i + 1)) { count++; i++; } str.append(input.charAt(i)).append(count); } return str.toString(); } if (mode == Mode.DECODE) { char[] stArr = input.toCharArray(); char lastChar = 0; for (int p = 0; p < stArr.length; p++) { char s = stArr[p]; if (!Character.isDigit(s)) { lastChar = s; str.append(s); } else { int e = digitCount(stArr, p); int n = Integer.parseInt(String.valueOf(input.substring(p, (p + e)))); for (int i = 0; i < n - 1; i++) { str.append(lastChar); } } } } return str.toString(); }  }
Rle { public static String rle(Mode mode, String input) { StringBuilder str = new StringBuilder(); if (mode == Mode.ENCODE) { int il = input.length(); for (int i = 0; i < il; i++) { int count = 1; while (i < il - 1 && input.charAt(i) == input.charAt(i + 1)) { count++; i++; } str.append(input.charAt(i)).append(count); } return str.toString(); } if (mode == Mode.DECODE) { char[] stArr = input.toCharArray(); char lastChar = 0; for (int p = 0; p < stArr.length; p++) { char s = stArr[p]; if (!Character.isDigit(s)) { lastChar = s; str.append(s); } else { int e = digitCount(stArr, p); int n = Integer.parseInt(String.valueOf(input.substring(p, (p + e)))); for (int i = 0; i < n - 1; i++) { str.append(lastChar); } } } } return str.toString(); }  static String rle(Mode mode, String input); static int digitCount(char[] stArr, int currentPos); }
Rle { public static String rle(Mode mode, String input) { StringBuilder str = new StringBuilder(); if (mode == Mode.ENCODE) { int il = input.length(); for (int i = 0; i < il; i++) { int count = 1; while (i < il - 1 && input.charAt(i) == input.charAt(i + 1)) { count++; i++; } str.append(input.charAt(i)).append(count); } return str.toString(); } if (mode == Mode.DECODE) { char[] stArr = input.toCharArray(); char lastChar = 0; for (int p = 0; p < stArr.length; p++) { char s = stArr[p]; if (!Character.isDigit(s)) { lastChar = s; str.append(s); } else { int e = digitCount(stArr, p); int n = Integer.parseInt(String.valueOf(input.substring(p, (p + e)))); for (int i = 0; i < n - 1; i++) { str.append(lastChar); } } } } return str.toString(); }  static String rle(Mode mode, String input); static int digitCount(char[] stArr, int currentPos);  }