-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProJinDian_1.5RepCharArray.java
More file actions
127 lines (114 loc) · 2.79 KB
/
Copy pathProJinDian_1.5RepCharArray.java
File metadata and controls
127 lines (114 loc) · 2.79 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
/*
* In a string, using '%20' replace blank. Only use original array.
* IMPORTANT: Edit for end to head. There are two scanning.
* First time, get the number of blank.
* Second time, replace blank from tail to head.
*/
// Solution One
public void replaceSpaces(char[] str, int length){
int spaceCount = 0, newLength, i;
for(i = 0; i < length; i++){
if(str[i] == ' '){
spaceCount++;
}
}
newLength = length + spaceCount * 2;
str[newLength] = '\0';
for(i = length - 1; i >= 0; i--){
if(str[i] == ' '){
str[newLength - 1] = '0';
str[newLength - 2] = '2';
str[newLength - 3] = '%';
newLength = newLength - 3;
}else{
str[newLength - 1] = str[i];
newLength = newLength - 1;
}
}
}
// Solution Two
String compressBetter(string str){
// Check the compressed string become longer or not
int size = countCompression(str);
if(size >= str.length()){
return str;
}
StringBuffer mystr = new SringBuffer();
char last = str.charAt(0);
int count = 1;
for(int i = 1; i < str.length(); i++){
if(str.charAt(i) == last){ // Find repeat char
count++;
}else{// Insert the number and update last char
mystr.append(last); // Insert char
mystr.append(count); // Insert number
last = str.charAt(i);
count = 1;
}
}
/*
* The last char havn't been inserted into the StringBuffer,
* because the if-else will stop at the end of String.
*/
mystr.append(last);
mystr.append(count);
return mystr.toString();
}
/*
* The method will be called before the the Compression actually happen
* Compare the lengths first, if is the result positive, call
* compression method, if not, then don't.
*/
int countCompression(String str){
if(str == null || str.isEmpty()) return 0;
char last = str.charAt(0);
int size = 0;
int count = 1;
for(int i = 1; i < str.length(); i++){
if(str.charAt(i) == last){
count++;
}else{
last = str.charAt(i);
size += 1 + String.valueOf(count).length();
count = 1;
}
}
size += 1 + String.valueOf(count).length();
return size;
}
// Solution 3: similar with solution 2, using array or not StringBuffer
String compressAlternate(String str){
int size = countCompression(str);
if(size >= str.length()){
return str;
}
char[] array = new char[size];
int inde = 0;
char last = str.charAt(0);
itn count = 1;
for(int i = 1; i < str.length(); i++){
if(str.charAt(i) == last){
count++;
}else{
// Update the repeat char number
index = setChar(array, last, index, count);
last = str.charAt(i);
count = 1;
}
}
// The last repeat char number
index = setChar(array, last, index, count);
return String.valueOf(array);
}
int setChar(char[] array, char c, int index, int count){
array[index] = c;
index ++;
//
char[] cnt = String.valueOf(count).toCharArray();
//
for(char x : cnt){
array[index] = x;
index++;
}
return index;
}