-
Notifications
You must be signed in to change notification settings - Fork 3
/
StringOne.java
631 lines (533 loc) · 19 KB
/
StringOne.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/**
*
*/
package coding.bat.solutions;
/**
* @author Aman Shekhar
*
*/
public class StringOne {
/**
* @param args
*/
public static void main(String[] args) {
}
// Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".
//
//
// helloName("Bob") → "Hello Bob!"
// helloName("Alice") → "Hello Alice!"
// helloName("X") → "Hello X!"
public String helloName(String name) {
return "Hello " + name + "!";
}
// --------------------------------------------------------------------------------------------
// Given two strings, a and b, return the result of putting them together in the
// order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi".
//
//
// makeAbba("Hi", "Bye") → "HiByeByeHi"
// makeAbba("Yo", "Alice") → "YoAliceAliceYo"
// makeAbba("What", "Up") → "WhatUpUpWhat"
public String makeAbba(String a, String b) {
return a + b + b + a;
}
// --------------------------------------------------------------------------------------------
// The web is built with HTML strings like "<i>Yay</i>" which draws Yay as
// italic text. In this example, the "i" tag makes <i> and </i> which surround
// the word "Yay". Given tag and word strings, create the HTML string with tags
// around the word, e.g. "<i>Yay</i>".
//
//
// makeTags("i", "Yay") → "<i>Yay</i>"
// makeTags("i", "Hello") → "<i>Hello</i>"
// makeTags("cite", "Yay") → "<cite>Yay</cite>"
public String makeTags(String tag, String word) {
return "<" + tag + ">" + word + "</" + tag + ">";
}
// --------------------------------------------------------------------------------------------
// Given an "out" string length 4, such as "<<>>", and a word, return a new
// string where the word is in the middle of the out string, e.g. "<<word>>".
// Note: use str.substring(i, j) to extract the String starting at index i and
// going up to but not including index j.
//
//
// makeOutWord("<<>>", "Yay") → "<<Yay>>"
// makeOutWord("<<>>", "WooHoo") → "<<WooHoo>>"
// makeOutWord("[[]]", "word") → "[[word]]"
//
public String makeOutWord(String out, String word) {
String firstPart = out.substring(0, 2);
String secondPart = out.substring(2, 4);
return firstPart + word + secondPart;
}
// --------------------------------------------------------------------------------------------
// Given a string, return a new string made of 3 copies of the last 2 chars of
// the original string. The string length will be at least 2.
//
//
// extraEnd("Hello") → "lololo"
// extraEnd("ab") → "ababab"
// extraEnd("Hi") → "HiHiHi"
public String extraEnd(String str) {
if (str.length() > 2) {
String mResult = str.substring(str.length() - 2, str.length());
return mResult + mResult + mResult;
}
return str + str + str;
}
// --------------------------------------------------------------------------------------------
// Given a string, return the string made of its first two chars, so the String
// "Hello" yields "He". If the string is shorter than length 2, return whatever
// there is, so "X" yields "X", and the empty string "" yields the empty string
// "". Note that str.length() returns the length of a string.
//
//
// firstTwo("Hello") → "He"
// firstTwo("abcdefg") → "ab"
// firstTwo("ab") → "ab"
public String firstTwo(String str) {
if (str.length() > 2)
return str.substring(0, 2);
return str;
}
// --------------------------------------------------------------------------------------------
// Given a string of even length, return the first half. So the string "WooHoo"
// yields "Woo".
//
//
// firstHalf("WooHoo") → "Woo"
// firstHalf("HelloThere") → "Hello"
// firstHalf("abcdef") → "abc"
public String firstHalf(String str) {
int length = str.length() / 2;
return str.substring(0, length);
}
// --------------------------------------------------------------------------------------------
// Given a string, return a version without the first and last char, so "Hello"
// yields "ell". The string length will be at least 2.
//
//
// withoutEnd("Hello") → "ell"
// withoutEnd("java") → "av"
// withoutEnd("coding") → "odin"
public String withoutEnd(String str) {
return str.substring(1, str.length() - 1);
}
// --------------------------------------------------------------------------------------------
// Given 2 strings, a and b, return a string of the form short+long+short, with
// the shorter string on the outside and the longer string on the inside. The
// strings will not be the same length, but they may be empty (length 0).
//
//
// comboString("Hello", "hi") → "hiHellohi"
// comboString("hi", "Hello") → "hiHellohi"
// comboString("aaa", "b") → "baaab"
//
public String comboString(String a, String b) {
if (a.length() > b.length())
return b + a + b;
return a + b + a;
}
// --------------------------------------------------------------------------------------------
// Given 2 strings, return their concatenation, except omit the first char of
// each. The strings will be at least length 1.
//
//
// nonStart("Hello", "There") → "ellohere"
// nonStart("java", "code") → "avaode"
// nonStart("shotl", "java") → "hotlava"
public String nonStart(String a, String b) {
return a.substring(1, a.length()) + b.substring(1, b.length());
}
// --------------------------------------------------------------------------------------------
// Given a string, return a "rotated left 2" version where the first 2 chars are
// moved to the end. The string length will be at least 2.
//
//
// left2("Hello") → "lloHe"
// left2("java") → "vaja"
// left2("Hi") → "Hi"
//
public String left2(String str) {
return str.substring(2, str.length()) + str.substring(0, 2);
}
// --------------------------------------------------------------------------------------------
// Given a string, return a "rotated right 2" version where the last 2 chars are
// moved to the start. The string length will be at least 2.
//
//
// right2("Hello") → "loHel"
// right2("java") → "vaja"
// right2("Hi") → "Hi"
//
public String right2(String str) {
int len = str.length() - 2;
return (str.substring(len) + str.substring(0, len));
}
// --------------------------------------------------------------------------------------------
//
// Given a string, return a string length 1 from its front, unless front is
// false, in which case return a string length 1 from its back. The string will
// be non-empty.
//
//
// theEnd("Hello", true) → "H"
// theEnd("Hello", false) → "o"
// theEnd("oh", true) → "o"
public String theEnd(String str, boolean front) {
if (front)
return str.substring(0, 1);
return str.substring(str.length() - 1, str.length());
}
// --------------------------------------------------------------------------------------------
// Given a string, return a version without both the first and last char of the
// string. The string may be any length, including 0.
//
//
// withouEnd2("Hello") → "ell"
// withouEnd2("abc") → "b"
// withouEnd2("ab") → ""
public String withouEnd2(String str) {
if (str.length() >= 3)
return str.substring(1, str.length() - 1);
return "";
}
// --------------------------------------------------------------------------------------------
// Given a string of even length, return a string made of the middle two chars,
// so the string "string" yields "ri". The string length will be at least 2.
//
//
// middleTwo("string") → "ri"
// middleTwo("code") → "od"
// middleTwo("Practice") → "ct"
public String middleTwo(String str) {
int halfLength = (str.length() / 2) - 1;
return str.substring(halfLength, halfLength + 2);
}
// --------------------------------------------------------------------------------------------
// Given a string, return true if it ends in "ly".
//
//
// endsLy("oddly") → true
// endsLy("y") → false
// endsLy("oddy") → false
public boolean endsLy(String str) {
if (str.length() < 2)
return false;
return str.substring(str.length() - 2).equals("ly");
}
// --------------------------------------------------------------------------------------------
// Given a string and an int n, return a string made of the first and last n
// chars from the string. The string length will be at least n.
//
//
// nTwice("Hello", 2) → "Helo"
// nTwice("Chocolate", 3) → "Choate"
// nTwice("Chocolate", 1) → "Ce"
public String nTwice(String str, int n) {
return str.substring(0, n) + str.substring(str.length() - n, str.length());
}
// --------------------------------------------------------------------------------------------
// Given a string and an index, return a string length 2 starting at the given
// index. If the index is too big or too small to define a string length 2, use
// the first 2 chars. The string length will be at least 2.
//
//
// twoChar("java", 0) → "ja"
// twoChar("java", 2) → "va"
// twoChar("java", 3) → "ja"
public String twoChar(String str, int index) {
if (index <= str.length() - 2 && index >= 0)
return str.substring(index, index + 2);
return str.substring(0, 2);
}
// --------------------------------------------------------------------------------------------
// Given a string of odd length, return the string length 3 from its middle, so
// "Candy" yields "and". The string length will be at least 3.
//
//
// middleThree("Candy") → "and"
// middleThree("and") → "and"
// middleThree("solving") → "lvi"
public String middleThree(String str) {
int length = str.length() / 2;
return str.substring(length - 1, length + 2);
}
// --------------------------------------------------------------------------------------------
// Given a string, return true if "bad" appears starting at index 0 or 1 in the
// string, such as with "badxxx" or "xbadxx" but not "xxbadxx". The string may
// be any length, including 0. Note: use .equals() to compare 2 strings.
//
//
// hasBad("badxx") → true
// hasBad("xbadxx") → true
// hasBad("xxbadxx") → false
//
public boolean hasBad(String str) {
if (str.length() == 3 && str.equals("bad"))
return true;
if (str.length() >= 4) {
return (str.substring(0, 3).equals("bad") || str.substring(1, 4).equals("bad"));
}
return false;
}
// --------------------------------------------------------------------------------------------
// Given a string, return a string length 2 made of its first 2 chars. If the
// string length is less than 2, use '@' for the missing chars.
//
//
// atFirst("hello") → "he"
// atFirst("hi") → "hi"
// atFirst("h") → "h@"
//
public String atFirst(String str) {
if (str.length() == 0)
return "@@";
if (str.length() < 2)
return str.substring(0, 1) + "@";
return str.substring(0, 2);
}
// --------------------------------------------------------------------------------------------
// Given 2 strings, a and b, return a new string made of the first char of a and
// the last char of b, so "yo" and "java" yields "ya". If either string is
// length 0, use '@' for its missing char.
//
//
// lastChars("last", "chars") → "ls"
// lastChars("yo", "java") → "ya"
// lastChars("hi", "") → "h@"
public String lastChars(String a, String b) {
if (a.length() == 0 && b.length() == 0)
return "@@";
if (a.length() == 0) {
return "@" + b.charAt(b.length() - 1);
} else if (b.length() == 0) {
return a.charAt(0) + "@";
}
return a.substring(0, 1) + b.substring(b.length() - 1);
}
// --------------------------------------------------------------------------------------------
// Given two strings, append them together (known as "concatenation") and return
// the result. However, if the concatenation creates a double-char, then omit
// one of the chars, so "abc" and "cat" yields "abcat".
//
//
// conCat("abc", "cat") → "abcat"
// conCat("dog", "cat") → "dogcat"
// conCat("abc", "") → "abc"
public String conCat(String a, String b) {
if (a.length() >= 1 && b.length() >= 1) {
if (a.charAt(a.length() - 1) == (b.charAt(0))) {
return a + b.substring(1, b.length());
}
}
return a + b;
}
// --------------------------------------------------------------------------------------------
// Given a string of any length, return a new string where the last 2 chars, if
// present, are swapped, so "coding" yields "codign".
//
//
// lastTwo("coding") → "codign"
// lastTwo("cat") → "cta"
// lastTwo("ab") → "ba"
public String lastTwo(String str) {
if (str.length() >= 2) {
return str.substring(0, str.length() - 2) + str.charAt(str.length() - 1) + str.charAt(str.length() - 2);
}
return str;
}
// --------------------------------------------------------------------------------------------
// Given a string, if the string begins with "red" or "blue" return that color
// string, otherwise return the empty string.
//
//
// seeColor("redxx") → "red"
// seeColor("xxred") → ""
// seeColor("blueTimes") → "blue"
public String seeColor(String str) {
if (str.length() == 3) {
if (str.substring(0, 3).equals("red"))
return "red";
return "";
}
if (str.length() >= 4) {
if (str.substring(0, 4).equals("blue"))
return "blue";
if (str.substring(0, 3).equals("red"))
return "red";
return "";
}
return "";
}
// --------------------------------------------------------------------------------------------
// Given a string, return true if the first 2 chars in the string also appear at
// the end of the string, such as with "edited".
//
//
// frontAgain("edited") → true
// frontAgain("edit") → false
// frontAgain("ed") → true
public boolean frontAgain(String str) {
int mLength = str.length();
if (mLength >= 2) {
return str.substring(0, 2).equals(str.substring(mLength - 2, mLength));
}
return false;
}
// --------------------------------------------------------------------------------------------
// Given two strings, append them together (known as "concatenation") and return
// the result. However, if the strings are different lengths, omit chars from
// the longer string so it is the same length as the shorter string. So "Hello"
// and "Hi" yield "loHi". The strings may be any length.
//
//
// minCat("Hello", "Hi") → "loHi"
// minCat("Hello", "java") → "ellojava"
// minCat("java", "Hello") → "javaello"
public String minCat(String a, String b) {
int lenA = a.length();
int lenB = b.length();
if (lenA >= lenB)
return (a.substring(lenA - lenB) + b);
else
return (a + b.substring(lenB - lenA));
}
// --------------------------------------------------------------------------------------------
// Given a string, return a new string made of 3 copies of the first 2 chars of
// the original string. The string may be any length. If there are fewer than 2
// chars, use whatever is there.
//
//
// extraFront("Hello") → "HeHeHe"
// extraFront("ab") → "ababab"
// extraFront("H") → "HHH"
//
public String extraFront(String str) {
if (str.length() >= 2) {
String mSubString = str.substring(0, 2);
return mSubString + mSubString + mSubString;
}
return str + str + str;
}
// --------------------------------------------------------------------------------------------
// Given a string, if a length 2 substring appears at both its beginning and
// end, return a string without the substring at the beginning, so "HelloHe"
// yields "lloHe". The substring may overlap with itself, so "Hi" yields "".
// Otherwise, return the original string unchanged.
//
//
// without2("HelloHe") → "lloHe"
// without2("HelloHi") → "HelloHi"
// without2("Hi") → ""
public String without2(String str) {
int mLength = str.length();
if (mLength >= 2) {
if (str.substring(0, 2).equals(str.substring(mLength - 2, mLength))) {
return str.substring(2, str.length());
}
return str;
}
return str;
}
// --------------------------------------------------------------------------------------------
// Given a string, return a version without the first 2 chars. Except keep the
// first char if it is 'a' and keep the second char if it is 'b'. The string may
// be any length. Harder than it looks.
//
//
// deFront("Hello") → "llo"
// deFront("java") → "va"
// deFront("away") → "aay"
public String deFront(String str) {
if ((str.length() == 1) && (str.charAt(0) == 'a')) {
return "a";
} else if (str.charAt(0) == 'a') {
if (str.charAt(1) == 'b') {
return str;
}
return "a" + str.substring(2, str.length());
} else if (str.charAt(1) == 'b') {
return "b" + str.substring(2, str.length());
}
return str.substring(2, str.length());
}
// --------------------------------------------------------------------------------------------
// Given a string and a second "word" string, we'll say that the word matches
// the string if it appears at the front of the string, except its first char
// does not need to match exactly. On a match, return the front of the string,
// or otherwise return the empty string. So, so with the string "hippo" the word
// "hi" returns "hi" and "xip" returns "hip". The word will be at least length
// 1.
//
//
// startWord("hippo", "hi") → "hi"
// startWord("hippo", "xip") → "hip"
// startWord("hippo", "i") → "h"
public String startWord(String str, String word) {
int lenStr = str.length();
int lenWord = word.length();
String temp;
if (lenStr >= lenWord) {
temp = str.substring(1, lenWord);
if (word.substring(1).equals(temp))
return (str.charAt(0) + temp);
return "";
}
return "";
}
// --------------------------------------------------------------------------------------------
// Given a string, if the first or last chars are 'x', return the string without
// those 'x' chars, and otherwise return the string unchanged.
//
//
// withoutX("xHix") → "Hi"
// withoutX("xHi") → "Hi"
// withoutX("Hxix") → "Hxi"
public String withoutX(String str) {
int len = str.length();
if (len >= 2) {
char ch = str.charAt(0);
StringBuilder stbuild = new StringBuilder(len);
if (ch != 'x')
stbuild.append(ch);
stbuild.append(str.substring(1, len - 1));
ch = str.charAt(len - 1);
if (ch != 'x')
stbuild.append(ch);
return stbuild.toString();
} else if (len == 1 && str.charAt(0) == 'x')
return "";
else
return str;
}
// --------------------------------------------------------------------------------------------
// Given a string, if one or both of the first 2 chars is 'x', return the string
// without those 'x' chars, and otherwise return the string unchanged. This is a
// little harder than it looks.
//
//
// withoutX2("xHi") → "Hi"
// withoutX2("Hxi") → "Hi"
// withoutX2("Hi") → "Hi"
public String withoutX2(String str) {
int len = str.length();
if (len == 0)
return str;
if (len == 1 && str.charAt(0) == 'x')
return "";
if (len >= 2) {
if (str.charAt(0) == 'x') {
if (str.charAt(1) != 'x')
return str.substring(1, len);
if (str.charAt(1) == 'x')
return str.substring(2, len);
} else if (str.charAt(1) == 'x') {
return str.charAt(0) + str.substring(2, len);
}
}
return str;
}
// --------------------------------------------------------------------------------------------
}