-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringII.java
337 lines (313 loc) · 9.68 KB
/
StringII.java
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import java.util.*;
public class StringII {
public static void main(String[] args) {
StringII s = new StringII();
// System.out.println(s.rightShift("abcdefg", 39));
System.out.println(s.allAnagrams("abc", "bacb"));
}
/**
* Reverse String
* <p>
* Reverse a given string
*/
public String reverse(String input) {
char[] ch = input.toCharArray();
reverse(ch, 0, ch.length - 1);
return new String(ch);
}
private void reverse(char[] ch, int a, int b) {
while (a < b) {
swap(ch, a++, b--);
}
}
private void swap(char[] ch, int a, int b) {
char temp = ch[a];
ch[a] = ch[b];
ch[b] = temp;
}
/**
* Reverse Words In A Sentence I
* <p>
* Reverse the words in a sentence
*/
public String reverseWords(String input) {
if (input == null) {
return null;
}
char[] ch = input.toCharArray();
reverse(ch, 0, ch.length - 1);
int i = 0;
int j = 0;
while (i < ch.length) {
while (j < ch.length && ch[j] != ' ') {
j++;
}
reverse(ch, i, j - 1);
i = j + 1;
j = i;
}
return new String(ch);
}
/**
* Right Shift By N Characters
* <p>
* Right shift a given string by n characters.
*/
public String rightShift(String input, int n) {
if (n == 0 || input.isEmpty()) {
return input;
}
n %= input.length();
char[] ch = input.toCharArray();
return new String(ch, n, ch.length - n) + new String(ch, 0, n);
}
/**
* String Replace (basic)
* <p>
* Given an original string input, and two strings S and T, from left to right
* replace all occurrences of S in input with T.
*/
public String replace(String input, String source, String target) {
char[] array = input.toCharArray();
if (source.length() > input.length()) {
return input;
}
if (target.length() > source.length()) {
return replaceLonger(array, source, target);
} else {
return replaceShorter(array, source, target);
}
}
private String replaceShorter(char[] array, String source, String target) {
int i = 0;
int j = 0;
while (i < array.length) {
if (isSubString(array, i, source)) {
i += source.length();
for (int k = 0; k < target.length(); k++) {
array[j++] = target.charAt(k);
}
} else {
array[j++] = array[i++];
}
}
return new String(array, 0, j);
}
private String replaceLonger(char[] array, String source, String target) {
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < array.length) {
if (isSubString(array, i, source)) {
i += source.length();
sb.append(target);
} else {
sb.append(array[i++]);
}
}
return sb.toString();
}
/**
* All Permutations II
* <p>
* Given a string with possible duplicate characters, return a list with all
* permutations of the characters.
*/
public List<String> permutations(String input) {
List<String> result = new ArrayList<>();
char[] ch = input.toCharArray();
StringBuilder sb = new StringBuilder();
permuations(ch, 0, sb, result);
return result;
}
private void permuations(char[] ch, int index, StringBuilder sb, List<String> result) {
if (index == ch.length) {
result.add(sb.toString());
return;
}
Set<Character> set = new HashSet<>();
for (int i = index; i < ch.length; i++) {
if (!set.contains(ch[i])) {
set.add(ch[i]);
swap(ch, index, i);
sb.append(ch[index]);
permuations(ch, index + 1, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
}
}
private boolean isSubString(char[] array, int start, String source) {
if (start + source.length() > array.length) {
return false;
}
for (int i = 0; i < source.length(); i++) {
if (array[i + start] != source.charAt(i)) {
return false;
}
}
return true;
}
/**
* ReOrder Array
* <p>
* Given an array of elements, reorder it as follow: * { N1, N2, N3, …, N2k } ->
* { N1, Nk+1, N2, Nk+2, N3, Nk+3, … , Nk, N2k } * { N1, N2, N3, …, N2k+1 } -> {
* N1, Nk+1, N2, Nk+2, N3, Nk+3, … , Nk, N2k, N2k+1 } Try to do it in place
*/
public int[] reorder(int[] array) {
int[] result = new int[array.length];
for (int i = 0; i < array.length / 2; i++) {
result[2 * i] = array[i];
result[2 * i + 1] = array[i + array.length / 2];
}
if (array.length % 2 == 1) {
result[array.length - 1] = array[array.length - 1];
}
return result;
}
/**
* Compress String II
* <p>
* Given a string, replace adjacent, repeated characters with the character
* followed by the number of repeated occurrences.
*/
public String compress(String input) {
char prev = (char) 0;
StringBuilder sb = new StringBuilder();
int cur = 0;
for (char c : input.toCharArray()) {
if (cur == 0) {
prev = c;
cur = 1;
continue;
}
if (prev == c) {
cur++;
} else {
sb.append(prev);
sb.append(cur);
cur = 1;
prev = c;
}
}
if (cur != 0) {
sb.append(prev);
sb.append(cur);
}
return sb.toString();
}
/**
* Decompress String II
* <p>
* Given a string in compressed form, decompress it to the original string. The
* adjacent repeated characters in the original string are compressed to have
* the character followed by the number of repeated occurrences.
*/
public String decompress(String input) {
char[] array = input.toCharArray();
int l = 0;
StringBuilder sb = new StringBuilder();
char prev = (char) 0;
for (char c : array) {
if (c - '0' < 10 && c - '0' >= 0) {
l *= 10;
l += c - '0';
} else {
while (l-- > 0) {
sb.append(prev);
}
l = 0;
prev = c;
}
}
while (l-- > 0) {
sb.append(prev);
}
return sb.toString();
}
/**
* Longest Substring Without Repeating Characters
* <p>
* Given a string, find the longest substring without any repeating characters
* and return the length of it. The input string is guaranteed to be not null.
* For example, the longest substring without repeating letters for "bcdfbd" is
* "bcdf", we should return 4 in this case.
*/
public int longest(String input) {
int max = 0;
boolean[] set = new boolean[26];
int i = 0;
int j = 0;
while (j < input.length()) {
if (!set[input.charAt(j) - 'a']) {
set[input.charAt(j++) - 'a'] = true;
max = Math.max(max, j - i);
} else {
while (input.charAt(i) != input.charAt(j)) {
set[input.charAt(i++) - 'a'] = false;
}
set[input.charAt(i++) - 'a'] = false;
set[input.charAt(j++) - 'a'] = true;
}
}
return max;
}
/**
* All Anagrams
* <p>
* Find all occurrence of anagrams of a given string s in a given string l.
* Return the list of starting indices.
*/
public List<Integer> allAnagrams(String sh, String lo) {
List<Integer> result = new ArrayList<>();
int[] set = new int[26];
if (sh.length() > lo.length()) {
return result;
}
for (int i = 0; i < sh.length(); i++) {
set[sh.charAt(i) - 'a']++;
}
int[] set2 = new int[26];
for (int i = 0; i < sh.length(); i++) {
set2[sh.charAt(i) - 'a']++;
}
if (Arrays.compare(set, set2) == 0) {
result.add(0);
}
for (int i = 1; i <= sh.length() - sh.length(); i++) {
set2[sh.charAt(i - 1) - 'a']--;
set2[sh.charAt(i - 1 + sh.length()) - 'a']++;
if (Arrays.compare(set, set2) == 0) {
result.add(i);
}
}
return result;
}
/**
* Longest subarray contains only 1s
* <p>
* Given an array of integers that contains only 0s and 1s and a positive integer k, you can flip at most k 0s to 1s, return the longest subarray that contains only integer 1 after flipping.
*/
public int longestConsecutiveOnes(int[] nums, int k) {
int i = 0;
int j = 0;
int max = 0;
while (j < nums.length) {
if (nums[j] == 1) {
j++;
} else {
if (k > 0) {
k--;
j++;
} else {
while (i < nums.length && nums[i] == 1) {
i++;
}
i++;
j++;
}
}
max = Math.max(j - i, max);
}
return max;
}
}