MCPcopy Create free account
hub / github.com/careercup/ctci / countCompression

Method countCompression

java/Chapter 1/Question1_5/Question.java:16–32  ·  view source on GitHub ↗
(String str)

Source from the content-addressed store, hash-verified

14 }
15
16 public static int countCompression(String str) {
17 if (str == null || str.isEmpty()) return 0;
18 char last = str.charAt(0);
19 int size = 0;
20 int count = 1;
21 for (int i = 1; i < str.length(); i++) {
22 if (str.charAt(i) == last) {
23 count++;
24 } else {
25 last = str.charAt(i);
26 size += 1 + String.valueOf(count).length();
27 count = 1;
28 }
29 }
30 size += 1 + String.valueOf(count).length();
31 return size;
32 }
33
34 public static String compressBad(String str) {
35 int size = countCompression(str);

Callers 4

compressBadMethod · 0.95
compressBetterMethod · 0.95
compressAlternateMethod · 0.95
mainMethod · 0.95

Calls 2

isEmptyMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected