-
Notifications
You must be signed in to change notification settings - Fork 3
/
WarmUp_Solution_II.java
372 lines (313 loc) · 10.3 KB
/
WarmUp_Solution_II.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package coding.bat.solutions;
public class WarmUp_Solution_Two {
public static void main(String[] args) {
}
// --------------------------------------------------------------------------------------------
// Given a string and a non-negative int n, return a larger string that is n
// copies of the original string.
//
//
// stringTimes("Hi", 2) → "HiHi"
// stringTimes("Hi", 3) → "HiHiHi"
// stringTimes("Hi", 1) → "Hi"
public String stringTimes(String str, int n) {
String result = "";
for (int i = 0; i < n; i++) {
result = result + str;
}
return result;
}
// --------------------------------------------------------------------------------------------
// Given a string and a non-negative int n, we'll say that the front of the
// string is the first 3 chars, or whatever is there if the string is less than
// length 3. Return n copies of the front;
//
//
// frontTimes("Chocolate", 2) → "ChoCho"
// frontTimes("Chocolate", 3) → "ChoChoCho"
// frontTimes("Abc", 3) → "AbcAbcAbc"
public String frontTimes(String str, int n) {
int frontLen = 3;
if (frontLen > str.length()) {
frontLen = str.length();
}
String front = str.substring(0, frontLen);
String result = "";
for (int i = 0; i < n; i++) {
result = result + front;
}
return result;
}
// --------------------------------------------------------------------------------------------
// Count the number of "xx" in the given string. We'll say that overlapping is
// allowed, so "xxx" contains 2 "xx".
// countXX("abcxx") → 1
// countXX("xxx") → 2
// countXX("xxxx") → 3
int countXX(String str) {
int count = 0;
for (int i = 0; i < str.length() - 1; i++) {
if (str.substring(i, i + 2).equals("xx"))
count++;
}
return count;
}
// --------------------------------------------------------------------------------------------
// Given a string, return true if the first instance of "x" in the string is
// immediately followed by another "x".
// doubleX("axxbb") → true
// doubleX("axaxax") → false
// doubleX("xxxxx") → true
boolean doubleX(String str) {
int i = str.indexOf("x");
if (i == -1)
return false;
if (i + 1 >= str.length())
return false;
return str.substring(i + 1, i + 2).equals("x");
}
// --------------------------------------------------------------------------------------------
// Given a string, return a new string made of every other char starting with
// the first, so "Hello" yields "Hlo".
//
//
// stringBits("Hello") → "Hlo"
// stringBits("Hi") → "H"
// stringBits("Heeololeo") → "Hello"
public String stringBits(String str) {
String result = "";
for (int i = 0; i < str.length(); i += 2) {
result = result + str.substring(i, i + 1);
}
return result;
}
// --------------------------------------------------------------------------------------------
// Given a non-empty string like "Code" return a string like "CCoCodCode".
//
//
// stringSplosion("Code") → "CCoCodCode"
// stringSplosion("abc") → "aababc"
// stringSplosion("ab") → "aab"
public String stringSplosion(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
result = result + str.substring(0, i + 1);
}
return result;
}
// --------------------------------------------------------------------------------------------
// Given a string, return the count of the number of times that a substring
// length 2 appears in the string and also as the last 2 chars of the string, so
// "hixxxhi" yields 1 (we won't count the end substring).
//
//
// last2("hixxhi") → 1
// last2("xaxxaxaxx") → 1
// last2("axxxaaxx") → 2
public int last2(String str) {
if (str.length() < 2)
return 0;
String end = str.substring(str.length() - 2);
int count = 0;
for (int i = 0; i < str.length() - 2; i++) {
String sub = str.substring(i, i + 2);
if (sub.equals(end))
count++;
}
return count;
}
// --------------------------------------------------------------------------------------------
//
// Given an array of ints, return the number of 9's in the array.
//
//
// arrayCount9([1, 2, 9]) → 1
// arrayCount9([1, 9, 9]) → 2
// arrayCount9([1, 9, 9, 3, 9]) → 3
//
public int arrayCount9(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 9) {
count++;
}
}
return count;
}
// --------------------------------------------------------------------------------------------
// Given an array of ints, return true if one of the first 4 elements in the
// array is a 9. The array length may be less than 4.
//
//
// arrayFront9([1, 2, 9, 3, 4]) → true
// arrayFront9([1, 2, 3, 4, 9]) → false
// arrayFront9([1, 2, 3, 4, 5]) → false
//
public boolean arrayFront9(int[] nums) {
int end = nums.length;
if (end > 4)
end = 4;
for (int i = 0; i < end; i++) {
if (nums[i] == 9)
return true;
}
return false;
}
// --------------------------------------------------------------------------------------------
// Given an array of ints, return true if the sequence of numbers 1, 2, 3
// appears in the array somewhere.
//
//
// array123([1, 1, 2, 3, 1]) → true
// array123([1, 1, 2, 4, 1]) → false
// array123([1, 1, 2, 1, 2, 3]) → true
//
public boolean array123(int[] nums) {
if (nums.length >= 3) {
for (int i = 0; i < nums.length - 2; i++) {
if ((nums[i] == 1) && (nums[i + 1] == 2) && (nums[i + 2] == 3))
return true;
}
}
return false;
}
// --------------------------------------------------------------------------------------------
// Given 2 strings, a and b, return the number of the positions where they
// contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3,
// since the "xx", "aa", and "az" substrings appear in the same place in both
// strings.
//
//
// stringMatch("xxcaazz", "xxbaaz") → 3
// stringMatch("abc", "abc") → 2
// stringMatch("abc", "axc") → 0
//
public int stringMatch(String a, String b) {
int len = Math.min(a.length(), b.length());
int count = 0;
for (int i = 0; i < len - 1; i++) {
String aSub = a.substring(i, i + 2);
String bSub = b.substring(i, i + 2);
if (aSub.equals(bSub)) {
count++;
}
}
return count;
}
// --------------------------------------------------------------------------------------------
// Given a string, return a version where all the "x" have been removed. Except
// an "x" at the very start or end should not be removed.
//
//
// stringX("xxHxix") → "xHix"
// stringX("abxxxcd") → "abcd"
// stringX("xabxxxcdx") → "xabcdx"
public String stringX(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
if (!(i > 0 && i < (str.length() - 1) && str.substring(i, i + 1).equals("x"))) {
result = result + str.substring(i, i + 1);
}
}
return result;
}
// --------------------------------------------------------------------------------------------
// Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9
// ... so "kittens" yields "kien".
//
//
// altPairs("kitten") → "kien"
// altPairs("Chocolate") → "Chole"
// altPairs("CodingHorror") → "Congrr"
//
//
public String altPairs(String str) {
String result = "";
for (int i = 0; i < str.length(); i += 4) {
int last = i + 2;
if (last > str.length())
last = str.length();
result = result + str.substring(i, last);
}
return result;
}
// --------------------------------------------------------------------------------------------
// Suppose the string "yak" is unlucky. Given a string, return a version where
// all the "yak" are removed, but the "a" can be any char. The "yak" strings
// will not overlap.
//
//
// stringYak("yakpak") → "pak"
// stringYak("pakyak") → "pak"
// stringYak("yak123ya") → "123ya"
//
public String stringYak(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
if (i + 2 < str.length() && str.charAt(i) == 'y' && str.charAt(i + 2) == 'k') {
i = i + 2;
} else {
result = result + str.charAt(i);
}
}
return result;
}
// --------------------------------------------------------------------------------------------
// Given an array of ints, return the number of times that two 6's are next to
// each other in the array. Also count instances where the second "6" is
// actually a 7.
// array667([6, 6, 2]) → 1
// array667([6, 6, 2, 6]) → 1
// array667([6, 7, 2, 6]) → 1
//
//
public int array667(int[] nums) {
int count = 0;
if (nums.length > 2) {
for (int i = 0; i < nums.length - 1; i++) {
if ((nums[i] == 6) && ((nums[i + 1] == 6) || (nums[i + 1] == 7))) {
count++;
}
}
}
return count;
}
// --------------------------------------------------------------------------------------------
// Given an array of ints, we'll say that a triple is a value appearing 3 times
// in a row in the array. Return true if the array does not contain any triples.
//
//
// noTriples([1, 1, 2, 2, 1]) → true
// noTriples([1, 1, 2, 2, 2, 1]) → false
// noTriples([1, 1, 1, 2, 2, 2, 1]) → false
//
public boolean noTriples(int[] nums) {
if (nums.length > 2) {
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] == nums[i + 1]) {
if (nums[i + 1] == nums[i + 2])
return false;
}
}
}
return true;
}
// --------------------------------------------------------------------------------------------
// Given an array of ints, return true if it contains a 2, 7, 1 pattern: a
// value, followed by the value plus 5, followed by the value minus 1.
// Additionally the 271 counts even if the "1" differs by 2 or less from the
// correct value.
// has271([1, 2, 7, 1]) → true
// has271([1, 2, 8, 1]) → false
// has271([2, 7, 1]) → true
public boolean has271(int[] nums) {
if (nums.length > 2) {
for (int i = 0; i < nums.length - 2; i++) {
int first = nums[i];
if ((first + 5 == nums[i + 1]) && (Math.abs(nums[i + 2] - (first - 1)) <= 2))
return true;
}
}
return false;
}
}