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

Method compressBetter

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

Source from the content-addressed store, hash-verified

52 }
53
54 public static String compressBetter(String str) {
55 int size = countCompression(str);
56 if (size >= str.length()) {
57 return str;
58 }
59 StringBuffer mystr = new StringBuffer();
60 char last = str.charAt(0);
61 int count = 1;
62 for (int i = 1; i < str.length(); i++) {
63 if (str.charAt(i) == last) {
64 count++;
65 } else {
66 mystr.append(last);
67 mystr.append(count);
68 last = str.charAt(i);
69 count = 1;
70 }
71 }
72 mystr.append(last);
73 mystr.append(count);
74 return mystr.toString();
75 }
76
77 public static String compressAlternate(String str) {
78 int size = countCompression(str);

Callers 1

mainMethod · 0.95

Calls 4

countCompressionMethod · 0.95
appendMethod · 0.80
lengthMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected