Skip to content

Commit 225c085

Browse files
Add files via upload
1 parent fabd5d5 commit 225c085

File tree

1 file changed

+372
-0
lines changed

1 file changed

+372
-0
lines changed

WarmUp_Solution_Two.java

Lines changed: 372 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
1+
package warmUp_1;
2+
3+
public class WarmUp_Solution_Two {
4+
5+
public static void main(String[] args) {
6+
7+
}
8+
9+
// --------------------------------------------------------------------------------------------
10+
11+
// Given a string and a non-negative int n, return a larger string that is n
12+
// copies of the original string.
13+
//
14+
//
15+
// stringTimes("Hi", 2) → "HiHi"
16+
// stringTimes("Hi", 3) → "HiHiHi"
17+
// stringTimes("Hi", 1) → "Hi"
18+
19+
public String stringTimes(String str, int n) {
20+
String result = "";
21+
for (int i = 0; i < n; i++) {
22+
result = result + str;
23+
}
24+
return result;
25+
}
26+
27+
// --------------------------------------------------------------------------------------------
28+
29+
// Given a string and a non-negative int n, we'll say that the front of the
30+
// string is the first 3 chars, or whatever is there if the string is less than
31+
// length 3. Return n copies of the front;
32+
//
33+
//
34+
// frontTimes("Chocolate", 2) → "ChoCho"
35+
// frontTimes("Chocolate", 3) → "ChoChoCho"
36+
// frontTimes("Abc", 3) → "AbcAbcAbc"
37+
38+
public String frontTimes(String str, int n) {
39+
int frontLen = 3;
40+
if (frontLen > str.length()) {
41+
frontLen = str.length();
42+
}
43+
String front = str.substring(0, frontLen);
44+
45+
String result = "";
46+
for (int i = 0; i < n; i++) {
47+
result = result + front;
48+
}
49+
return result;
50+
}
51+
52+
// --------------------------------------------------------------------------------------------
53+
54+
// Count the number of "xx" in the given string. We'll say that overlapping is
55+
// allowed, so "xxx" contains 2 "xx".
56+
57+
// countXX("abcxx") → 1
58+
// countXX("xxx") → 2
59+
// countXX("xxxx") → 3
60+
61+
int countXX(String str) {
62+
int count = 0;
63+
for (int i = 0; i < str.length() - 1; i++) {
64+
if (str.substring(i, i + 2).equals("xx"))
65+
count++;
66+
}
67+
return count;
68+
}
69+
70+
// --------------------------------------------------------------------------------------------
71+
72+
// Given a string, return true if the first instance of "x" in the string is
73+
// immediately followed by another "x".
74+
75+
// doubleX("axxbb") → true
76+
// doubleX("axaxax") → false
77+
// doubleX("xxxxx") → true
78+
79+
boolean doubleX(String str) {
80+
int i = str.indexOf("x");
81+
if (i == -1)
82+
return false;
83+
if (i + 1 >= str.length())
84+
return false;
85+
return str.substring(i + 1, i + 2).equals("x");
86+
}
87+
88+
// --------------------------------------------------------------------------------------------
89+
90+
// Given a string, return a new string made of every other char starting with
91+
// the first, so "Hello" yields "Hlo".
92+
//
93+
//
94+
// stringBits("Hello") → "Hlo"
95+
// stringBits("Hi") → "H"
96+
// stringBits("Heeololeo") → "Hello"
97+
98+
public String stringBits(String str) {
99+
String result = "";
100+
for (int i = 0; i < str.length(); i += 2) {
101+
result = result + str.substring(i, i + 1);
102+
}
103+
return result;
104+
}
105+
106+
// --------------------------------------------------------------------------------------------
107+
108+
// Given a non-empty string like "Code" return a string like "CCoCodCode".
109+
//
110+
//
111+
// stringSplosion("Code") → "CCoCodCode"
112+
// stringSplosion("abc") → "aababc"
113+
// stringSplosion("ab") → "aab"
114+
115+
public String stringSplosion(String str) {
116+
String result = "";
117+
for (int i = 0; i < str.length(); i++) {
118+
result = result + str.substring(0, i + 1);
119+
}
120+
return result;
121+
}
122+
123+
// --------------------------------------------------------------------------------------------
124+
125+
// Given a string, return the count of the number of times that a substring
126+
// length 2 appears in the string and also as the last 2 chars of the string, so
127+
// "hixxxhi" yields 1 (we won't count the end substring).
128+
//
129+
//
130+
// last2("hixxhi") → 1
131+
// last2("xaxxaxaxx") → 1
132+
// last2("axxxaaxx") → 2
133+
134+
public int last2(String str) {
135+
if (str.length() < 2)
136+
return 0;
137+
138+
String end = str.substring(str.length() - 2);
139+
int count = 0;
140+
for (int i = 0; i < str.length() - 2; i++) {
141+
String sub = str.substring(i, i + 2);
142+
if (sub.equals(end))
143+
count++;
144+
}
145+
return count;
146+
}
147+
148+
// --------------------------------------------------------------------------------------------
149+
150+
//
151+
// Given an array of ints, return the number of 9's in the array.
152+
//
153+
//
154+
// arrayCount9([1, 2, 9]) → 1
155+
// arrayCount9([1, 9, 9]) → 2
156+
// arrayCount9([1, 9, 9, 3, 9]) → 3
157+
//
158+
public int arrayCount9(int[] nums) {
159+
int count = 0;
160+
for (int i = 0; i < nums.length; i++) {
161+
if (nums[i] == 9) {
162+
count++;
163+
}
164+
}
165+
return count;
166+
}
167+
168+
// --------------------------------------------------------------------------------------------
169+
170+
// Given an array of ints, return true if one of the first 4 elements in the
171+
// array is a 9. The array length may be less than 4.
172+
//
173+
//
174+
// arrayFront9([1, 2, 9, 3, 4]) → true
175+
// arrayFront9([1, 2, 3, 4, 9]) → false
176+
// arrayFront9([1, 2, 3, 4, 5]) → false
177+
//
178+
public boolean arrayFront9(int[] nums) {
179+
180+
int end = nums.length;
181+
if (end > 4)
182+
end = 4;
183+
184+
for (int i = 0; i < end; i++) {
185+
if (nums[i] == 9)
186+
return true;
187+
}
188+
189+
return false;
190+
}
191+
192+
// --------------------------------------------------------------------------------------------
193+
194+
// Given an array of ints, return true if the sequence of numbers 1, 2, 3
195+
// appears in the array somewhere.
196+
//
197+
//
198+
// array123([1, 1, 2, 3, 1]) → true
199+
// array123([1, 1, 2, 4, 1]) → false
200+
// array123([1, 1, 2, 1, 2, 3]) → true
201+
//
202+
public boolean array123(int[] nums) {
203+
if (nums.length >= 3) {
204+
for (int i = 0; i < nums.length - 2; i++) {
205+
if ((nums[i] == 1) && (nums[i + 1] == 2) && (nums[i + 2] == 3))
206+
return true;
207+
}
208+
}
209+
return false;
210+
}
211+
212+
// --------------------------------------------------------------------------------------------
213+
214+
// Given 2 strings, a and b, return the number of the positions where they
215+
// contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3,
216+
// since the "xx", "aa", and "az" substrings appear in the same place in both
217+
// strings.
218+
//
219+
//
220+
// stringMatch("xxcaazz", "xxbaaz") → 3
221+
// stringMatch("abc", "abc") → 2
222+
// stringMatch("abc", "axc") → 0
223+
//
224+
public int stringMatch(String a, String b) {
225+
int len = Math.min(a.length(), b.length());
226+
int count = 0;
227+
for (int i = 0; i < len - 1; i++) {
228+
String aSub = a.substring(i, i + 2);
229+
String bSub = b.substring(i, i + 2);
230+
if (aSub.equals(bSub)) {
231+
count++;
232+
}
233+
}
234+
235+
return count;
236+
}
237+
238+
// --------------------------------------------------------------------------------------------
239+
240+
// Given a string, return a version where all the "x" have been removed. Except
241+
// an "x" at the very start or end should not be removed.
242+
//
243+
//
244+
// stringX("xxHxix") → "xHix"
245+
// stringX("abxxxcd") → "abcd"
246+
// stringX("xabxxxcdx") → "xabcdx"
247+
248+
public String stringX(String str) {
249+
String result = "";
250+
for (int i = 0; i < str.length(); i++) {
251+
if (!(i > 0 && i < (str.length() - 1) && str.substring(i, i + 1).equals("x"))) {
252+
result = result + str.substring(i, i + 1);
253+
}
254+
}
255+
return result;
256+
}
257+
258+
// --------------------------------------------------------------------------------------------
259+
260+
// Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9
261+
// ... so "kittens" yields "kien".
262+
//
263+
//
264+
// altPairs("kitten") → "kien"
265+
// altPairs("Chocolate") → "Chole"
266+
// altPairs("CodingHorror") → "Congrr"
267+
//
268+
//
269+
public String altPairs(String str) {
270+
String result = "";
271+
for (int i = 0; i < str.length(); i += 4) {
272+
int last = i + 2;
273+
if (last > str.length())
274+
last = str.length();
275+
result = result + str.substring(i, last);
276+
}
277+
return result;
278+
}
279+
280+
// --------------------------------------------------------------------------------------------
281+
282+
// Suppose the string "yak" is unlucky. Given a string, return a version where
283+
// all the "yak" are removed, but the "a" can be any char. The "yak" strings
284+
// will not overlap.
285+
//
286+
//
287+
// stringYak("yakpak") → "pak"
288+
// stringYak("pakyak") → "pak"
289+
// stringYak("yak123ya") → "123ya"
290+
//
291+
292+
public String stringYak(String str) {
293+
String result = "";
294+
295+
for (int i = 0; i < str.length(); i++) {
296+
if (i + 2 < str.length() && str.charAt(i) == 'y' && str.charAt(i + 2) == 'k') {
297+
i = i + 2;
298+
} else {
299+
result = result + str.charAt(i);
300+
}
301+
}
302+
return result;
303+
}
304+
305+
// --------------------------------------------------------------------------------------------
306+
307+
// Given an array of ints, return the number of times that two 6's are next to
308+
// each other in the array. Also count instances where the second "6" is
309+
// actually a 7.
310+
311+
// array667([6, 6, 2]) → 1
312+
// array667([6, 6, 2, 6]) → 1
313+
// array667([6, 7, 2, 6]) → 1
314+
//
315+
//
316+
public int array667(int[] nums) {
317+
int count = 0;
318+
if (nums.length > 2) {
319+
for (int i = 0; i < nums.length - 1; i++) {
320+
if ((nums[i] == 6) && ((nums[i + 1] == 6) || (nums[i + 1] == 7))) {
321+
count++;
322+
}
323+
}
324+
}
325+
return count;
326+
}
327+
328+
// --------------------------------------------------------------------------------------------
329+
330+
// Given an array of ints, we'll say that a triple is a value appearing 3 times
331+
// in a row in the array. Return true if the array does not contain any triples.
332+
//
333+
//
334+
// noTriples([1, 1, 2, 2, 1]) → true
335+
// noTriples([1, 1, 2, 2, 2, 1]) → false
336+
// noTriples([1, 1, 1, 2, 2, 2, 1]) → false
337+
//
338+
public boolean noTriples(int[] nums) {
339+
if (nums.length > 2) {
340+
for (int i = 0; i < nums.length - 2; i++) {
341+
if (nums[i] == nums[i + 1]) {
342+
if (nums[i + 1] == nums[i + 2])
343+
return false;
344+
}
345+
}
346+
}
347+
return true;
348+
}
349+
350+
// --------------------------------------------------------------------------------------------
351+
352+
// Given an array of ints, return true if it contains a 2, 7, 1 pattern: a
353+
// value, followed by the value plus 5, followed by the value minus 1.
354+
// Additionally the 271 counts even if the "1" differs by 2 or less from the
355+
// correct value.
356+
357+
// has271([1, 2, 7, 1]) → true
358+
// has271([1, 2, 8, 1]) → false
359+
// has271([2, 7, 1]) → true
360+
361+
public boolean has271(int[] nums) {
362+
if (nums.length > 2) {
363+
for (int i = 0; i < nums.length - 2; i++) {
364+
int first = nums[i];
365+
if ((first + 5 == nums[i + 1]) && (Math.abs(nums[i + 2] - (first - 1)) <= 2))
366+
return true;
367+
}
368+
}
369+
return false;
370+
}
371+
372+
}

0 commit comments

Comments
 (0)