-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from Kara3/master
Strings Compression Solution (fixed Issue #34)
- Loading branch information
Showing
1 changed file
with
26 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,35 @@ | ||
public class StringCompression { | ||
|
||
void printCompressedString(String s) | ||
{ | ||
for (int i = 0; i < s.length(); i++) { | ||
int count = 1; | ||
while (i < s.length() - 1 && s.charAt(i) == s.charAt((i++) + 1)) | ||
count++; | ||
void printCompressedString(String input) { | ||
String compressedWord = ""; | ||
boolean areTheySame = false; | ||
int counter = 1; | ||
|
||
System.out.print(s.charAt(i)); | ||
System.out.print(count); | ||
for (int index = 0; index < input.length(); index++) { | ||
final char letter = input.charAt(index); | ||
if (index < input.length() - 1) { | ||
final char nextLetter = input.charAt(index + 1); | ||
if (letter == nextLetter) { | ||
counter++; | ||
areTheySame = true; | ||
} else if (areTheySame) { | ||
compressedWord += String.valueOf(letter) + counter; | ||
counter = 1; | ||
areTheySame = false; | ||
} else { | ||
compressedWord += String.valueOf(letter) + counter; | ||
} | ||
} else { | ||
compressedWord += String.valueOf(letter) + counter; | ||
} | ||
} | ||
System.out.println(compressedWord); | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
StringCompression sc = new StringCompression(); | ||
String s = "hhhaaaccckkktttooobbbeeerrr"; | ||
sc.printCompressedString(s); | ||
StringCompression compression = new StringCompression(); | ||
String input = "hhhaaaccckkktttooobbbeeerrr"; | ||
compression.printCompressedString(input); | ||
} | ||
} |