-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy path_01_06_StringCompression.java
More file actions
153 lines (148 loc) · 5.18 KB
/
_01_06_StringCompression.java
File metadata and controls
153 lines (148 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package arraystring;
/**
* Implement a method to perform basic string compression using the counts of repeated characters.
* For example, the string aabcccccaaa would become a2blc5a3.
* If the "compressed" string would not become smaller than the original string, your method should return
* the original string. You can assume the string has only uppercase and lowercase letters (a - z).
*/
class _01_06_StringCompression {
String compress(String s) {
StringBuilder sb = new StringBuilder();
char c = ' ';
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != c) {
if (count > 0) {
sb.append(c).append(count);
}
c = s.charAt(i);
count = 1;
}
else {
count++;
}
}
if (count > 0) {
sb.append(c).append(count);
}
return sb.length() < s.length() ? sb.toString() : s;
}
/**
* Given an array of characters, compress it in-place.
*
* The length after compression must always be smaller than or equal to the original array.
*
* Every element of the array should be a character (not int) of length 1.
*
* After you are done modifying the input array in-place, return the new length of the array.
*
* Example 1:
*
* Input:
* ["a","a","b","b","c","c","c"]
*
* Output:
* Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
*
* Explanation:
* "aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
*
* Example 2:
*
* Input:
* ["a"]
*
* Output:
* Return 1, and the first 1 characters of the input array should be: ["a"]
*
* Explanation:
* Nothing is replaced.
*
* Example 3:
*
* Input:
* ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
*
* Output:
* Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
*
* Explanation:
* Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
*
* @param chars
* @return
*/
public int compress_leetcode_443(char[] chars) {
if (chars.length <= 1) return chars.length;
char c = chars[0];
int count = 0;
int j = 0;
for (int i = 1; i <= chars.length; i++) {
if (i < chars.length && chars[i] == c) { // repeating characters
count++;
}
else if (i >= chars.length || c != chars[i]) { // to the end or encounter a new character
chars[j++] = c;
if (count > 0) {
String s = String.valueOf(count + 1);
System.arraycopy(s.toCharArray(), 0, chars, j, s.length());
j += s.length();
}
if (i < chars.length) {
c = chars[i];
count = 0;
}
}
}
return j;
}
public int compress_leetcode_443_2nd(char[] chars) {
if (chars.length <= 1) return chars.length;
char c = chars[0];
int count = 0;
int j = 0;
for (int i = 1; i < chars.length; i++) {
if (chars[i] == c) { // count repeating characters
count++;
}
if (i + 1 == chars.length || c != chars[i]) { // to the end of the chars or a different character
chars[j++] = c; // start to output last character
if (count > 0) { // append count of repeating times to the character
String s = String.valueOf(count + 1);
System.arraycopy(s.toCharArray(), 0, chars, j, s.length());
j += s.length();
}
if (i + 1 == chars.length && chars[i] != c) {
chars[j++] = chars[i];
}
c = chars[i];
count = 0;
}
}
return j;
}
public int compress_leetcode_443_best(char[] chars) {
int anchor = 0, write = 0;
for (int read = 0; read < chars.length; read++) {
if ((read + 1 == chars.length) || (chars[read + 1] != chars[read])) {
chars[write++] = chars[anchor];
if (read > anchor) {
for (char c : Integer.toString(read - anchor + 1).toCharArray()) {
chars[write++] = c;
}
}
anchor = read + 1;
}
}
return write;
}
public static void main(String args[]) {
_01_06_StringCompression solution = new _01_06_StringCompression();
char[] chars = "aabbcccd".toCharArray();
int len = solution.compress_leetcode_443(chars);
System.out.println("After compress_mine: " + new String(chars).substring(0, len));
chars = "aabbcccd".toCharArray();
len = solution.compress_leetcode_443_best(chars);
System.out.println("After compress_best: " + new String(chars).substring(0, len));
}
}