Created
June 21, 2014 05:18
-
-
Save bitcpf/f8a3615255a8808bddc6 to your computer and use it in GitHub Desktop.
CC150
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class compression { | |
public static String stringcompression(String in_string) { | |
String result = ""; | |
int s_len = in_string.length(); | |
int i=0; | |
int char_cnt = 1; | |
while(i<s_len-1){ | |
if (in_string.charAt(i)==in_string.charAt(i+1)){ | |
char_cnt ++; | |
} | |
else { | |
result = result + in_string.substring(i, i+1); | |
result = result + Integer.toString(char_cnt); | |
char_cnt = 1; | |
} | |
i ++; | |
} | |
result = result + in_string.substring(i, i+1); | |
result = result + Integer.toString(char_cnt); | |
if (result.length() < s_len){ | |
return result; | |
} | |
else{ | |
return in_string; | |
} | |
} | |
public static void main(String[] args) { | |
String input = "aabcccccaaa"; | |
System.out.println(stringcompression(input)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment