@Test public void shouldNotCompressEmptyWords() { String wordToCompress = ""; String result = compressString.compress(wordToCompress); assertEquals("", result); }
public String compress(String src) { validateInput(src); if (src.isEmpty() || src.length() == 1) { return src; } StringBuilder stringBuilder = new StringBuilder(); int repeatedCharCounter = 1; char previousChar = src.charAt(0); for (int i = 1; i < src.length(); i++) { char currentChar = src.charAt(i); if (isCharRepeated(previousChar, currentChar)) { repeatedCharCounter++; } else { addChar(stringBuilder, previousChar); if (repeatedCharCounter > 1) { stringBuilder.append(repeatedCharCounter); repeatedCharCounter = 1; } previousChar = currentChar; } } appendLastCharIfNeeded(stringBuilder, repeatedCharCounter, previousChar); return stringBuilder.toString(); }
CompressString { public String compress(String src) { validateInput(src); if (src.isEmpty() || src.length() == 1) { return src; } StringBuilder stringBuilder = new StringBuilder(); int repeatedCharCounter = 1; char previousChar = src.charAt(0); for (int i = 1; i < src.length(); i++) { char currentChar = src.charAt(i); if (isCharRepeated(previousChar, currentChar)) { repeatedCharCounter++; } else { addChar(stringBuilder, previousChar); if (repeatedCharCounter > 1) { stringBuilder.append(repeatedCharCounter); repeatedCharCounter = 1; } previousChar = currentChar; } } appendLastCharIfNeeded(stringBuilder, repeatedCharCounter, previousChar); return stringBuilder.toString(); } }
CompressString { public String compress(String src) { validateInput(src); if (src.isEmpty() || src.length() == 1) { return src; } StringBuilder stringBuilder = new StringBuilder(); int repeatedCharCounter = 1; char previousChar = src.charAt(0); for (int i = 1; i < src.length(); i++) { char currentChar = src.charAt(i); if (isCharRepeated(previousChar, currentChar)) { repeatedCharCounter++; } else { addChar(stringBuilder, previousChar); if (repeatedCharCounter > 1) { stringBuilder.append(repeatedCharCounter); repeatedCharCounter = 1; } previousChar = currentChar; } } appendLastCharIfNeeded(stringBuilder, repeatedCharCounter, previousChar); return stringBuilder.toString(); }  }
CompressString { public String compress(String src) { validateInput(src); if (src.isEmpty() || src.length() == 1) { return src; } StringBuilder stringBuilder = new StringBuilder(); int repeatedCharCounter = 1; char previousChar = src.charAt(0); for (int i = 1; i < src.length(); i++) { char currentChar = src.charAt(i); if (isCharRepeated(previousChar, currentChar)) { repeatedCharCounter++; } else { addChar(stringBuilder, previousChar); if (repeatedCharCounter > 1) { stringBuilder.append(repeatedCharCounter); repeatedCharCounter = 1; } previousChar = currentChar; } } appendLastCharIfNeeded(stringBuilder, repeatedCharCounter, previousChar); return stringBuilder.toString(); }  String compress(String src); String compressRecursive(String src); }
CompressString { public String compress(String src) { validateInput(src); if (src.isEmpty() || src.length() == 1) { return src; } StringBuilder stringBuilder = new StringBuilder(); int repeatedCharCounter = 1; char previousChar = src.charAt(0); for (int i = 1; i < src.length(); i++) { char currentChar = src.charAt(i); if (isCharRepeated(previousChar, currentChar)) { repeatedCharCounter++; } else { addChar(stringBuilder, previousChar); if (repeatedCharCounter > 1) { stringBuilder.append(repeatedCharCounter); repeatedCharCounter = 1; } previousChar = currentChar; } } appendLastCharIfNeeded(stringBuilder, repeatedCharCounter, previousChar); return stringBuilder.toString(); }  String compress(String src); String compressRecursive(String src);  }