Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- System.out.println(compress("aaaabbsddddddd"));
- }
- // String compression
- // aaaabbsddddddd == a4b2sd7
- static String compress(String str) {
- String result = "";
- int i = 0;
- int c;
- char prev;
- if (str.length() > 0) {
- while(i < str.length()) {
- c = 1;
- prev = str.charAt(i);
- i++;
- while ((i < str.length()) && (str.charAt(i) == prev)) {
- c++;
- i++;
- }
- if(c == 1){
- result += prev;
- } else{
- result += prev + "" + c;
- }
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement