- Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathReorganizeString767.java
More file actions
Latest commit
167 lines (152 loc) · 4.76 KB
/
Copy pathReorganizeString767.java
File metadata and controls
167 lines (152 loc) · 4.76 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Given a string S, check if the letters can be rearranged so that two
* characters that are adjacent to each other are not the same.
*
* If possible, output any possible result. If not possible, return the
* empty string.
*
* Example 1:
* Input: S = "aab"
* Output: "aba"
*
* Example 2:
* Input: S = "aaab"
* Output: ""
*
* Note:
* S will consist of lowercase letters and have length in range [1, 500].
*/
publicclassReorganizeString767 {
publicStringreorganizeString(StringS) {
int[] hash = newint[26];
for(charc : S.toCharArray()) hash[c-'a']++;
intmax = 0;
for(inti = 0; i<26; ++i){
if(hash[i] > hash[max]) max = i;
}
if(hash[max] > (S.length()+1)/2) return"";
char[] res = newchar[S.length()];
intptr = 0;
while(hash[max]-- > 0){
res[ptr] = (char)(max + 'a');
ptr += 2;
}
for(inti = 0; i<26; ++i){
while(hash[i]-- > 0){
if(ptr >= S.length()) ptr = 1;
res[ptr] = (char)(i + 'a');
ptr += 2;
}
}
returnnewString(res);
}
/**
* https://leetcode.com/problems/reorganize-string/solution/
*/
publicStringreorganizeString2(StringS) {
intN = S.length();
int[] count = newint[26];
for (charc: S.toCharArray()) count[c-'a']++;
PriorityQueue<MultiChar> pq = newPriorityQueue<MultiChar>((a, b) ->
a.count == b.count ? a.letter - b.letter : b.count - a.count);
for (inti = 0; i < 26; ++i) if (count[i] > 0) {
if (count[i] > (N + 1) / 2) return"";
pq.add(newMultiChar(count[i], (char) ('a' + i)));
}
StringBuilderans = newStringBuilder();
while (pq.size() >= 2) {
MultiCharmc1 = pq.poll();
MultiCharmc2 = pq.poll();
/*This code turns out to be superfluous, but explains what is happening
if (ans.length() == 0 || mc1.letter != ans.charAt(ans.length() - 1)) {
ans.append(mc1.letter);
ans.append(mc2.letter);
} else {
ans.append(mc2.letter);
ans.append(mc1.letter);
}*/
ans.append(mc1.letter);
ans.append(mc2.letter);
if (--mc1.count > 0) pq.add(mc1);
if (--mc2.count > 0) pq.add(mc2);
}
if (pq.size() > 0) ans.append(pq.poll().letter);
returnans.toString();
}
classMultiChar {
intcount;
charletter;
MultiChar(intct, charch) {
count = ct;
letter = ch;
}
}
// use 358. Rearrange String k Distance Apart
publicStringreorganizeString3(StringS) {
returnrearrangeString(S, 2);
}
publicStringrearrangeString(Stringstr, intk) {
intlength = str.length();
int[] count = newint[26];
int[] nextIndex = newint[26];
char[] chars = str.toCharArray();
for (charch: chars){
count[ch-'a']++;
}
StringBuildersb = newStringBuilder();
for (inti=0; i<length; i++) {
intidx = findValidMax(count, nextIndex, i);
if (idx == -1) return"";
sb.append((char) (idx + 'a'));
count[idx]--;
nextIndex[idx] = i+k;
}
returnsb.toString();
}
privateintfindValidMax(int[] count, int[] nextIndex, intindex){
intmax = Integer.MIN_VALUE;
intidx = -1;
for (inti=0; i<26; i++) {
if (count[i] != 0 && count[i] > max && index >= nextIndex[i]) {
max = count[i];
idx = i;
}
}
returnidx;
}
/**
* https://leetcode.com/problems/reorganize-string/discuss/113451/7-ms-Java-O(n)-Solution.-no-Sorting
*/
publicStringreorganizeString4(StringS) {
intn = S.length();
int[] cnt = newint[128];
charmc = 'a';
for (charc : S.toCharArray()) {
cnt[c]++;
mc = (cnt[c] > cnt[mc]) ? c : mc;
}
if (cnt[mc] == 1) {
returnS;
}
if (n - cnt[mc] <= cnt[mc] - 2) {
return"";
}
StringBuilder[] sb = newStringBuilder[cnt[mc]];
for (inti = 0; i < sb.length; i ++) {
sb[i] = newStringBuilder();
sb[i].append(mc);
}
intk = 0;
for (charc = 'a'; c <= 'z'; c++) {
while (c != mc && cnt[c] > 0) {
sb[k++].append(c);
cnt[c]--;
k %= sb.length;
}
}
for (inti = 1; i < sb.length; i++) {
sb[0].append(sb[i]);
}
returnsb[0].toString();
}
}