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

Method compressBad

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

Source from the content-addressed store, hash-verified

32 }
33
34 public static String compressBad(String str) {
35 int size = countCompression(str);
36 if (size >= str.length()) {
37 return str;
38 }
39 String mystr = "";
40 char last = str.charAt(0);
41 int count = 1;
42 for (int i = 1; i < str.length(); i++) {
43 if (str.charAt(i) == last) {
44 count++;
45 } else {
46 mystr += last + "" + count;
47 last = str.charAt(i);
48 count = 1;
49 }
50 }
51 return mystr + last + count;
52 }
53
54 public static String compressBetter(String str) {
55 int size = countCompression(str);

Callers

nothing calls this directly

Calls 2

countCompressionMethod · 0.95
lengthMethod · 0.45

Tested by

no test coverage detected