Last active
August 29, 2015 14:16
-
-
Save EnesKorukcu/b740e63032f3facd7873 to your computer and use it in GitHub Desktop.
Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string.
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
/** | |
* Solution 1 | |
* Bad Solution | |
* Time Complexity: 0(p + k2), where p is the size of the original string and k is the number of character sequences | |
*/ | |
public static String compressBad(String input) { | |
String output = ""; | |
int charCount = 0; | |
char[] charArray = input.toCharArray(); | |
char temp = input.charAt(0); | |
for(char c : charArray) { | |
if(temp == c) { | |
charCount++; | |
} else { | |
output += temp + String.valueOf(charCount); | |
charCount = 1; | |
temp = c; | |
} | |
} | |
output += temp + String.valueOf(charCount); | |
if(output.length() > input.length()) { | |
return input; | |
} | |
return output; | |
} | |
/** | |
* Solution 2 - With StringBuffer | |
* Time Complexity: O(n) | |
* Space Complexity: O(n) | |
*/ | |
public static String compressBetter1(String input) { | |
int size = countCompression(input); | |
if(size >= input.length()) { | |
return input; | |
} | |
StringBuffer stringBuffer = new StringBuffer(); | |
char last = input.charAt(0); | |
int count = 1; | |
for(int i = 1; i < input.length(); i++) { | |
if(input.charAt(i) == last) { | |
count++; | |
} else { | |
stringBuffer.append(last); | |
stringBuffer.append(count); | |
last = input.charAt(i); | |
count = 1; | |
} | |
} | |
stringBuffer.append(last); | |
stringBuffer.append(count); | |
return stringBuffer.toString(); | |
} | |
/** | |
* Solution 3 - Without StringBuffer | |
* Time Complexity: O(n) | |
* Space Complexity: O(n) | |
*/ | |
public static String compressBetter2(String input) { | |
int size = countCompression(input); | |
if(size >= input.length()) { | |
return input; | |
} | |
char[] charArray = new char[size]; | |
int index = 0; | |
char last = input.charAt(0); | |
int count = 1; | |
for(int i = 1; i < input.length(); i++) { | |
if(input.charAt(i) == last) { | |
count++; | |
} else { | |
index = setChar(charArray, last, index, count); | |
last = input.charAt(i); | |
count = 1; | |
} | |
} | |
index = setChar(charArray, last, index, count); | |
return String.valueOf(charArray); | |
} | |
/** | |
* Helper Function | |
* | |
*/ | |
public static int countCompression(String input) { | |
if(input == null || input.isEmpty()) { | |
return 0; | |
} | |
char last = input.charAt(0); | |
int size = 0; | |
int count = 1; | |
for(int i = 1; i < input.length(); i++) { | |
if(input.charAt(i) == last) { | |
count++; | |
} else { | |
last = input.charAt(i); | |
size += 1 + String.valueOf(count).length(); | |
count = 1; | |
} | |
} | |
size += 1 + String.valueOf(count).length(); | |
return size; | |
} | |
/** | |
* Helper Function | |
* | |
*/ | |
public static int setChar(char[] array, char c, int index, int count) { | |
array[index] = c; | |
index++; | |
char[] cnt = String.valueOf(count).toCharArray(); | |
for(char x : cnt) { | |
array[index] = x; | |
index++; | |
} | |
return index; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment