SHARE
TWEET

Untitled

a guest Nov 12th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public static String shrink(String input) {
  2.         if (input == null || input.length() == 0) return "";
  3.         StringBuilder sb = new StringBuilder();
  4.         char[] chars = input.toCharArray();
  5.  
  6.         sb.append(chars[0]);
  7.         int counter = 1;
  8.         for (int i = 1; i < chars.length; i++) {
  9.             if (chars[i] == chars[i-1]) {
  10.                 counter++;
  11.             } else {
  12.                 if (counter > 1) sb.append(counter);
  13.                 sb.append(chars[i]);
  14.                 counter = 1;
  15.             }
  16.         }
  17.         if (counter > 1) sb.append(counter);
  18.  
  19.         return sb.toString();
  20.     }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top