-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStringThree.java
283 lines (247 loc) · 8.45 KB
/
StringThree.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
/**
*
*/
package coding.bat.solutions;
/**
* @author Aman Shekhar
*
*/
public class StringThree {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
// Given a string, count the number of words ending in 'y' or 'z' -- so the 'y'
// in "heavy" and the 'z' in "fez" count, but not the 'y' in "yellow" (not case
// sensitive). We'll say that a y or z is at the end of a word if there is not
// an alphabetic letter immediately following it. (Note:
// Character.isLetter(char) tests if a char is an alphabetic letter.)
//
//
// countYZ("fez day") → 2
// countYZ("day fez") → 2
// countYZ("day fyyyz") → 2
public int countYZ(String str) {
int count = 0;
str = str.toLowerCase() + " ";
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) == 'y' || str.charAt(i) == 'z') && !Character.isLetter(str.charAt(i + 1))) {
count++;
}
}
return count;
}
// --------------------------------------------------------------------------------------------
// Given two strings, base and remove, return a version of the base string where
// all instances of the remove string have been removed (not case sensitive).
// You may assume that the remove string is length 1 or more. Remove only
// non-overlapping instances, so with "xxx" removing "xx" leaves "x".
//
//
// withoutString("Hello there", "llo") → "He there"
// withoutString("Hello there", "e") → "Hllo thr"
// withoutString("Hello there", "x") → "Hello there"
public String withoutString(String base, String remove) {
String result = "";
int index = base.toLowerCase().indexOf(remove.toLowerCase());
while (index != -1) {
result += base.substring(0, index);
base = base.substring(index + remove.length());
index = base.toLowerCase().indexOf(remove.toLowerCase());
}
result += base;
return result;
}
// --------------------------------------------------------------------------------------------
// Given a string, return true if the number of appearances of "is" anywhere in
// the string is equal to the number of appearances of "not" anywhere in the
// string (case sensitive).
//
//
// equalIsNot("This is not") → false
// equalIsNot("This is notnot") → true
// equalIsNot("noisxxnotyynotxisi") → true
//
public boolean equalIsNot(String str) {
int countIs = 0;
int countNot = 0;
str = str + "X";
for (int i = 0; i < str.length() - 2; i++) {
if (str.substring(i, i + 2).equals("is"))
countIs++;
if (str.substring(i, i + 3).equals("not"))
countNot++;
}
return (countIs == countNot);
}
// --------------------------------------------------------------------------------------------
// We'll say that a lowercase 'g' in a string is "happy" if there is another 'g'
// immediately to its left or right. Return true if all the g's in the given
// string are happy.
//
//
// gHappy("xxggxx") → true
// gHappy("xxgxx") → false
// gHappy("xxggyygxx") → false
public boolean gHappy(String str) {
str = "X" + str + "X";
for (int i = 0; i < str.length() - 1; i++)
if (str.charAt(i) == 'g' && str.charAt(i - 1) != 'g' && str.charAt(i + 1) != 'g')
return false;
return true;
}
// --------------------------------------------------------------------------------------------
// We'll say that a "triple" in a string is a char appearing three times in a
// row. Return the number of triples in the given string. The triples may
// overlap.
//
//
// countTriple("abcXXXabc") → 1
// countTriple("xxxabyyyycd") → 3
// countTriple("a") → 0
public int countTriple(String str) {
int count = 0;
for (int i = 0; i < str.length() - 2; i++) {
if ((str.charAt(i) == str.charAt(i + 1)) && str.charAt(i + 1) == str.charAt(i + 2))
count++;
}
return count;
}
// --------------------------------------------------------------------------------------------
// Given a string, return the sum of the digits 0-9 that appear in the string,
// ignoring all other characters. Return 0 if there are no digits in the string.
// (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1',
// .. '9'. Integer.parseInt(string) converts a string to an int.)
//
//
// sumDigits("aa1bc2d3") → 6
// sumDigits("aa11b33") → 8
// sumDigits("Chocolate") → 0
public int sumDigits(String str) {
int sum = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i)))
sum += Integer.parseInt(str.substring(i, i + 1));
}
return sum;
}
// --------------------------------------------------------------------------------------------
// Given a string, return the longest substring that appears at both the
// beginning and end of the string without overlapping. For example,
// sameEnds("abXab") is "ab".
//
//
// sameEnds("abXYab") → "ab"
// sameEnds("xx") → "x"
// sameEnds("xxx") → "x"
public String sameEnds(String string) {
String result = "";
int len = string.length();
for (int i = 0; i <= len / 2; i++)
for (int j = len / 2; j < len; j++)
if (string.substring(0, i).equals(string.substring(j)))
result = string.substring(0, i);
return result;
}
// --------------------------------------------------------------------------------------------
// Given a string, look for a mirror image (backwards) string at both the
// beginning and end of the given string. In other words, zero or more
// characters at the very begining of the given string, and at the very end of
// the string in reverse order (possibly overlapping). For example, the string
// "abXYZba" has the mirror end "ab".
//
//
// mirrorEnds("abXYZba") → "ab"
// mirrorEnds("abca") → "a"
// mirrorEnds("aba") → "aba"
public String mirrorEnds(String string) {
String result = "";
int len = string.length();
for (int i = 0, j = len - 1; i < len; i++, j--)
if (string.charAt(i) == string.charAt(j))
result += string.charAt(i);
else
break;
return result;
}
// --------------------------------------------------------------------------------------------
// Given a string, return the length of the largest "block" in the string. A
// block is a run of adjacent chars that are the same.
//
//
// maxBlock("hoopla") → 2
// maxBlock("abbCCCddBBBxx") → 3
// maxBlock("") → 0
public int maxBlock(String str) {
int mMax = 0;
int mLength = str.length();
for (int i = 0; i < mLength; i++) {
int mCount = 0;
for (int j = i; j < mLength; j++) {
if (str.charAt(i) == str.charAt(j)) {
mCount++;
} else {
break;
}
}
if (mCount > mMax)
mMax = mCount;
}
return mMax;
}
// --------------------------------------------------------------------------------------------
// Given a string, return the sum of the numbers appearing in the string,
// ignoring all other characters. A number is a series of 1 or more digit chars
// in a row. (Note: Character.isDigit(char) tests if a char is one of the chars
// '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.)
//
//
// sumNumbers("abc123xyz") → 123
// sumNumbers("aa11b33") → 44
// sumNumbers("7 11") → 18
public int sumNumbers(String str) {
int sum = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
int count = 0;
for (int j = i; j < str.length(); j++) {
if (Character.isDigit(str.charAt(j)))
count++;
else
break;
}
sum += Integer.parseInt(str.substring(i, i + count));
i += count;
}
}
return sum;
}
// --------------------------------------------------------------------------------------------
// Given a string, return a string where every appearance of the lowercase word
// "is" has been replaced with "is not". The word "is" should not be immediately
// preceeded or followed by a letter -- so for example the "is" in "this" does
// not count. (Note: Character.isLetter(char) tests if a char is a letter.)
//
//
// notReplace("is test") → "is not test"
// notReplace("is-is") → "is not-is not"
// notReplace("This is right") → "This is not right"
public String notReplace(String str) {
String result = "";
str = " " + str + " "; // avoid issues with corner cases
for (int i = 0; i < str.length() - 2; i++) {
if (str.charAt(i) == 'i') {
if (str.charAt(i + 1) == 's' && !Character.isLetter(str.charAt(i + 2))
&& !Character.isLetter(str.charAt(i - 1))) {
result += "is not";
i += 1;
} else
result += "i";
} else
result += str.charAt(i);
}
return result.substring(1);
}
}