-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathStringsAndThings.java
More file actions
164 lines (130 loc) · 5.51 KB
/
StringsAndThings.java
File metadata and controls
164 lines (130 loc) · 5.51 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
package io.zipcoder;
import java.util.Arrays;
/**
* @author tariq
*/
public class StringsAndThings {
public static void main(String[] args) {
}
/**
* 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 static Integer countYZ(String input) {
Integer count = 0;
String[] word = input.split(" "); //Split string by white spaces
String arrayString = Arrays.toString(word); //created new array
String newString;
Character lastLetter;
//System.out.println(arrayString);
for (int i = 0; i < word.length; i++) {
newString = word[i]; // assigned each word to newString
lastLetter = newString.charAt(newString.length() - 1); // assigned last letter of each word to lastLetter Character variable
//System.out.println(lastLetter);
if (lastLetter.equals('y') || lastLetter.equals('z')) { // Adds to count if "y" or "z" is found
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".
* <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 static String removeString(String base, String remove) {
Integer baseLength = base.length();
Integer removeLength = remove.length();
char[] charArray = base.toCharArray();
Integer j = 0;
for (int i = 0; i < baseLength; i++) {
if (i < baseLength - removeLength + 1 && base.substring(i, i + removeLength).equals(remove) ) {
i += removeLength - 1;
} else {
charArray[j] = base.charAt(i);
j++;
}
}
String str = String.valueOf(charArray);
String returnStr = str.substring(0, j);
return returnStr;
}
/**
* 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 static Boolean containsEqualNumberOfIsAndNot(String input) {
Integer countNot = 0;
Integer countIs = 0;
for (int i = 0; i < input.length(); i++) {
if (i < input.length() - 2 && input.substring(i, i + 3).equals("not")) {
countNot++;
i += 2;
} else if (i < input.length() - 1 && input.substring(i, i + 2).equals("is")) {
countIs++;
i += 1;
}
}
return countNot == countIs;
}
/**
* 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 static Boolean gIsHappy(String input) { // one of the tests didn't pass don't really know why?? will solve later
Boolean happy = true;
for (int i = 0; i < input.length(); i++) {
if (input.length() == 1 && input.charAt(i) == 'g') {
happy = false;
break;
}
else if (i < input.length() - 1
&& input.charAt(i) == 'g'
&& input.charAt(i + 1) != 'g'
&& input.charAt(i - 1) != 'g')
{
happy = false;
break;
} else if (i == input.length() - 1
&& input.charAt(i) == 'g'
&& input.charAt(i - 1) != 'g') {
happy = false;
break;
}
}
return happy;
}
/**
* 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) {
Integer strLength = input.length();
int count = 0;
for (int i = 0; i < strLength-2; i++) {
char tmp = input.charAt(i);
if (tmp == input.charAt(i+1) && tmp == input.charAt(i+2)) {
count++;
}
}
return count;
}
}