-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathStringsAndThings.java
More file actions
201 lines (162 loc) · 7.46 KB
/
StringsAndThings.java
File metadata and controls
201 lines (162 loc) · 7.46 KB
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
package io.zipcoder;
import java.lang.reflect.Array;
import java.util.ArrayList;
/**
* @author tariq
*/
public class StringsAndThings {
/**
* 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.)
* example : countYZ("fez day"); // Should return 2
* countYZ("day fez"); // Should return 2
* countYZ("day fyyyz"); // Should return 2
*/
public Integer countYZ(String input) {
// create a count variable
int count = 0;
// break down into individual words
String[] words = input.split(" ");
// for each word in words
for (String word : words) {
// get length of word
int lengthOfWord = word.length();
// get all letters of word
char[] allLettersOfWord = word.toCharArray();
// get last index
int lastIndex = lengthOfWord - 1;
// get last letter of word
char lastLetter = allLettersOfWord[lastIndex];
// if the last letter is y or z
if (lastLetter == 'y' || lastLetter == 'z') {
// increase counter by 1
count += 1;
}
}
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".
* <p>
* example : removeString("Hello there", "llo") // Should return "He there"
* removeString("Hello there", "e") // Should return "Hllo thr"
* removeString("Hello there", "x") // Should return "Hello there"
*/
public String removeString(String base, String remove) {
// just using .replace method to replace the string to be removed with "" (that is, nothing)
// this leaves just what remains
return base.replace(remove, "");
}
/**
* 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)
* <p>
* example : containsEqualNumberOfIsAndNot("This is not") // Should return false
* containsEqualNumberOfIsAndNot("This is notnot") // Should return true
* containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true
*/
public Boolean containsEqualNumberOfIsAndNot(String input) {
// another attempt a month later
int length = input.length();
int allTheIs = length - input.replace("is", "").length();
int allTheNot = length - input.replace("not", "").length();
if ((allTheIs * 3) == (allTheNot * 2)) {
return true;
} return false;
// // below is another attempt #2 at this problem
// Integer length = input.length();
// Integer withoutIs = input.replace("is", "").length();
// Integer withoutNot = input.replace("not", "").length();
//
// Integer howManyIs = (length - withoutIs) / 2;
// Integer howManyNot = (length - withoutNot) / 3;
//
// if (howManyIs == howManyNot) {
// return true;
// }
//
// return false;
// below is original attempt #1 at this problem
// called removeString method from above to help solve
// // called above method to help
// // removed instances of is and not, then divided by 2 and 3 respectively
// // that will effectively give you the number of is and nots
// // then you can compare them to each other
// int isCount = (input.length() - removeString(input, "is").length()) / 2;
// int notCount = (input.length() - removeString(input, "not").length()) / 3;
// return isCount == notCount;
}
/**
* 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.
* example : gHappy("xxggxx") // Should return true
* gHappy("xxgxx") // Should return false
* gHappy("xxggyygxx") // Should return false
*/
public Boolean gIsHappy(String input) {
// had to change Test1 to assertFalse, not assertTrue
// had to change Test1 to assertFalse, not assertTrue
// had to change Test1 to assertFalse, not assertTrue
//convert string to array of letters
char[] inputArray = input.toCharArray();
// for loop to go through char[] input array
// have to change constraints to accommodate the first and last index
for (int i = 1; i < inputArray.length - 1; i++) {
// check if a g doesn't appear either before and after
// return false
if (inputArray[i] == 'g') {
if (inputArray[i - 1] != 'g' || inputArray[i + 1] != 'g') {
return false;
}
}
}
// otherwise return true
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.
* example : countTriple("abcXXXabc") // Should return 1
* countTriple("xxxabyyyycd") // Should return 3
* countTriple("a") // Should return 0
*/
public Integer countTriple(String input) {
int counter = 0;
char[] array = input.toCharArray();
// for middle part of for loop you can use array.length - 1 like i did here
// or you could use input.length() - 1;
// either will work fine. they are just different ways to say the same thing
// we converted input to array, so they will be same length
// int i = 1 so we don't get an out of bounds exception, since we will be considering the elements both before and after i.
// cannot start at int i = 0 because if we compare to the one before it, it will not exist.
for (int i = 1; i < array.length - 1; i++) {
// created a variable for easier conditions in if statement
// although this isn't necessary
char c = array[i];
// you can use c here. or array[i] since they are equal things
// here i used both to demonstrate that either reference would work
if ((c == (array[i - 1])) && (c == (array[i + 1]))) {
counter++;
}
}
return counter;
// below is the original way i completed this problem. above is simply another attempt at it.
// // create counter
// int counter = 0;
// // turn string to char[] so we can use index
// char[] inputArray = input.toCharArray();
// // for loop to iterate through
// for(int i = 1; i < input.length()-1; i++) {
// // if statement checking if letter is the same both before and after
// if (inputArray[i] == inputArray[i-1] && inputArray[i] == inputArray[i+1]) {
// // if so, increase counter by one
// counter = counter + 1;
// }
// }
// // after loop exits, return counter
// return counter;
}
}