SHARE
TWEET

Untitled

a guest Jan 25th, 2020 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Solution {
  2.   public String decompress(String input) {
  3.     char[] array = input.toCharArray();
  4.     int[] requiredLen = new int[1];
  5.     int lastValidIdx = initReplace(array, requiredLen);
  6.     if (requiredLen[0] > array.length) {
  7.       array = copy(array, requiredLen[0]);
  8.     }
  9.     secondReplace(array, lastValidIdx);
  10.     return new String(array, array.length - requiredLen[0], requiredLen[0]);
  11.   }
  12.   //a1c0b2c4 --> ab2c4___
  13.   private int initReplace(char[] array, int[] requiredLen) {
  14.     int slow = 0;
  15.     for (int fast = 1; fast < array.length; fast += 2) {
  16.       char cur = array[fast];
  17.       int count = cur - '0';
  18.       if (count == 0) {
  19.         continue;
  20.       }
  21.       requiredLen[0] += count;
  22.       array[slow++] = array[fast - 1];
  23.       if (count == 1) { // just copy the char
  24.         continue;
  25.       }
  26.       array[slow++] = cur; // copy the digit
  27.     }
  28.     return slow - 1;
  29.   }
  30.  
  31.   private int initReplace(char[] array, int[] requiredLen) {
  32.     int slow = 0, fast = 1;;
  33.     while (fast < array.length) {
  34.       StringBuilder sb = new StringBuilder();
  35.       while (fast < array.length && Character.isDigit(array[fast])) {
  36.         sb.append(array[fast++]);
  37.       }
  38.       String countStr = sb.toString();
  39.       int count = String.parserInt(countStr);
  40.       if (count == 0) {
  41.         continue;
  42.       }
  43.       requiredLen[0] += count;
  44.       // b256 --> b256
  45.       array[slow++] = array[fast - 1];
  46.       if (count == 1){
  47.         continue;
  48.       }
  49.       int i = 0;
  50.       while (i < countStr.length()) {
  51.         array[slow++] = countStr.charAt(i++);
  52.       }
  53.     }
  54.     return slow - 1;
  55.   }
  56.   //ab2c4___ --> xxxxcccc
  57.   private void secondReplace(char[] array, int right) {
  58.     int tail = array.length - 1;
  59.     while (tail >= 0 && right >= 0) {
  60.       char cur = array[right];
  61.       if (Character.isDigit(cur)) { // it is a digit, so copy count times of the prev char
  62.         int count =  cur - '0';
  63.         while (count > 0) {
  64.           array[tail--] = array[right - 1];
  65.           count--;
  66.         }
  67.         right -= 2;
  68.       } else {// not a digit, just copy
  69.         array[tail--] = cur;
  70.         right -= 1;
  71.       }
  72.     }
  73.   }
  74.  
  75.   private char[] copy(char[] shorter, int newLength) {
  76.     char[] longer = new char[newLength];
  77.     for (int i = 0; i < shorter.length; i++) {
  78.       longer[i] = shorter[i];
  79.     }
  80.     return longer;
  81.   }
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top