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

Method compressAlternate

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

Source from the content-addressed store, hash-verified

75 }
76
77 public static String compressAlternate(String str) {
78 int size = countCompression(str);
79 if (size >= str.length()) {
80 return str;
81 }
82 char[] array = new char[size];
83 int index = 0;
84 char last = str.charAt(0);
85 int count = 1;
86 for (int i = 1; i < str.length(); i++) {
87 if (str.charAt(i) == last) {
88 count++;
89 } else {
90 index = setChar(array, last, index, count);
91 last = str.charAt(i);
92 count = 1;
93 }
94 }
95 index = setChar(array, last, index, count);
96 return String.valueOf(array);
97 }
98
99 public static void main(String[] args) {
100 String str = "abbccccccde";

Callers 1

mainMethod · 0.95

Calls 3

countCompressionMethod · 0.95
setCharMethod · 0.95
lengthMethod · 0.45

Tested by

no test coverage detected