|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package coding.bat.solutions; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author Aman Shekhar |
| 8 | + * |
| 9 | + */ |
| 10 | +public class StringThree { |
| 11 | + |
| 12 | + /** |
| 13 | + * @param args |
| 14 | + */ |
| 15 | + public static void main(String[] args) { |
| 16 | + // TODO Auto-generated method stub |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + // Given a string, count the number of words ending in 'y' or 'z' -- so the 'y' |
| 21 | + // in "heavy" and the 'z' in "fez" count, but not the 'y' in "yellow" (not case |
| 22 | + // sensitive). We'll say that a y or z is at the end of a word if there is not |
| 23 | + // an alphabetic letter immediately following it. (Note: |
| 24 | + // Character.isLetter(char) tests if a char is an alphabetic letter.) |
| 25 | + // |
| 26 | + // |
| 27 | + // countYZ("fez day") → 2 |
| 28 | + // countYZ("day fez") → 2 |
| 29 | + // countYZ("day fyyyz") → 2 |
| 30 | + |
| 31 | + public int countYZ(String str) { |
| 32 | + int count = 0; |
| 33 | + str = str.toLowerCase() + " "; |
| 34 | + for (int i = 0; i < str.length(); i++) { |
| 35 | + if ((str.charAt(i) == 'y' || str.charAt(i) == 'z') && !Character.isLetter(str.charAt(i + 1))) { |
| 36 | + count++; |
| 37 | + } |
| 38 | + } |
| 39 | + return count; |
| 40 | + } |
| 41 | + |
| 42 | + // -------------------------------------------------------------------------------------------- |
| 43 | + |
| 44 | + // Given two strings, base and remove, return a version of the base string where |
| 45 | + // all instances of the remove string have been removed (not case sensitive). |
| 46 | + // You may assume that the remove string is length 1 or more. Remove only |
| 47 | + // non-overlapping instances, so with "xxx" removing "xx" leaves "x". |
| 48 | + // |
| 49 | + // |
| 50 | + // withoutString("Hello there", "llo") → "He there" |
| 51 | + // withoutString("Hello there", "e") → "Hllo thr" |
| 52 | + // withoutString("Hello there", "x") → "Hello there" |
| 53 | + |
| 54 | + public String withoutString(String base, String remove) { |
| 55 | + String result = ""; |
| 56 | + int index = base.toLowerCase().indexOf(remove.toLowerCase()); |
| 57 | + while (index != -1) { |
| 58 | + result += base.substring(0, index); |
| 59 | + base = base.substring(index + remove.length()); |
| 60 | + index = base.toLowerCase().indexOf(remove.toLowerCase()); |
| 61 | + } |
| 62 | + result += base; |
| 63 | + |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + // -------------------------------------------------------------------------------------------- |
| 68 | + |
| 69 | + // Given a string, return true if the number of appearances of "is" anywhere in |
| 70 | + // the string is equal to the number of appearances of "not" anywhere in the |
| 71 | + // string (case sensitive). |
| 72 | + // |
| 73 | + // |
| 74 | + // equalIsNot("This is not") → false |
| 75 | + // equalIsNot("This is notnot") → true |
| 76 | + // equalIsNot("noisxxnotyynotxisi") → true |
| 77 | + // |
| 78 | + public boolean equalIsNot(String str) { |
| 79 | + int countIs = 0; |
| 80 | + int countNot = 0; |
| 81 | + str = str + "X"; |
| 82 | + for (int i = 0; i < str.length() - 2; i++) { |
| 83 | + if (str.substring(i, i + 2).equals("is")) |
| 84 | + countIs++; |
| 85 | + if (str.substring(i, i + 3).equals("not")) |
| 86 | + countNot++; |
| 87 | + } |
| 88 | + return (countIs == countNot); |
| 89 | + } |
| 90 | + |
| 91 | + // -------------------------------------------------------------------------------------------- |
| 92 | + |
| 93 | + // We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' |
| 94 | + // immediately to its left or right. Return true if all the g's in the given |
| 95 | + // string are happy. |
| 96 | + // |
| 97 | + // |
| 98 | + // gHappy("xxggxx") → true |
| 99 | + // gHappy("xxgxx") → false |
| 100 | + // gHappy("xxggyygxx") → false |
| 101 | + |
| 102 | + public boolean gHappy(String str) { |
| 103 | + str = "X" + str + "X"; |
| 104 | + for (int i = 0; i < str.length() - 1; i++) |
| 105 | + if (str.charAt(i) == 'g' && str.charAt(i - 1) != 'g' && str.charAt(i + 1) != 'g') |
| 106 | + return false; |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + // -------------------------------------------------------------------------------------------- |
| 111 | + |
| 112 | + // We'll say that a "triple" in a string is a char appearing three times in a |
| 113 | + // row. Return the number of triples in the given string. The triples may |
| 114 | + // overlap. |
| 115 | + // |
| 116 | + // |
| 117 | + // countTriple("abcXXXabc") → 1 |
| 118 | + // countTriple("xxxabyyyycd") → 3 |
| 119 | + // countTriple("a") → 0 |
| 120 | + |
| 121 | + public int countTriple(String str) { |
| 122 | + int count = 0; |
| 123 | + for (int i = 0; i < str.length() - 2; i++) { |
| 124 | + if ((str.charAt(i) == str.charAt(i + 1)) && str.charAt(i + 1) == str.charAt(i + 2)) |
| 125 | + count++; |
| 126 | + } |
| 127 | + return count; |
| 128 | + } |
| 129 | + |
| 130 | + // -------------------------------------------------------------------------------------------- |
| 131 | + |
| 132 | + // Given a string, return the sum of the digits 0-9 that appear in the string, |
| 133 | + // ignoring all other characters. Return 0 if there are no digits in the string. |
| 134 | + // (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', |
| 135 | + // .. '9'. Integer.parseInt(string) converts a string to an int.) |
| 136 | + // |
| 137 | + // |
| 138 | + // sumDigits("aa1bc2d3") → 6 |
| 139 | + // sumDigits("aa11b33") → 8 |
| 140 | + // sumDigits("Chocolate") → 0 |
| 141 | + |
| 142 | + public int sumDigits(String str) { |
| 143 | + int sum = 0; |
| 144 | + for (int i = 0; i < str.length(); i++) { |
| 145 | + if (Character.isDigit(str.charAt(i))) |
| 146 | + sum += Integer.parseInt(str.substring(i, i + 1)); |
| 147 | + } |
| 148 | + return sum; |
| 149 | + } |
| 150 | + |
| 151 | + // -------------------------------------------------------------------------------------------- |
| 152 | + |
| 153 | + // Given a string, return the longest substring that appears at both the |
| 154 | + // beginning and end of the string without overlapping. For example, |
| 155 | + // sameEnds("abXab") is "ab". |
| 156 | + // |
| 157 | + // |
| 158 | + // sameEnds("abXYab") → "ab" |
| 159 | + // sameEnds("xx") → "x" |
| 160 | + // sameEnds("xxx") → "x" |
| 161 | + |
| 162 | + public String sameEnds(String string) { |
| 163 | + String result = ""; |
| 164 | + int len = string.length(); |
| 165 | + for (int i = 0; i <= len / 2; i++) |
| 166 | + for (int j = len / 2; j < len; j++) |
| 167 | + if (string.substring(0, i).equals(string.substring(j))) |
| 168 | + result = string.substring(0, i); |
| 169 | + return result; |
| 170 | + } |
| 171 | + |
| 172 | + // -------------------------------------------------------------------------------------------- |
| 173 | + |
| 174 | + // Given a string, look for a mirror image (backwards) string at both the |
| 175 | + // beginning and end of the given string. In other words, zero or more |
| 176 | + // characters at the very begining of the given string, and at the very end of |
| 177 | + // the string in reverse order (possibly overlapping). For example, the string |
| 178 | + // "abXYZba" has the mirror end "ab". |
| 179 | + // |
| 180 | + // |
| 181 | + // mirrorEnds("abXYZba") → "ab" |
| 182 | + // mirrorEnds("abca") → "a" |
| 183 | + // mirrorEnds("aba") → "aba" |
| 184 | + |
| 185 | + public String mirrorEnds(String string) { |
| 186 | + String result = ""; |
| 187 | + int len = string.length(); |
| 188 | + for (int i = 0, j = len - 1; i < len; i++, j--) |
| 189 | + if (string.charAt(i) == string.charAt(j)) |
| 190 | + result += string.charAt(i); |
| 191 | + else |
| 192 | + break; |
| 193 | + return result; |
| 194 | + } |
| 195 | + |
| 196 | + // -------------------------------------------------------------------------------------------- |
| 197 | + |
| 198 | + // Given a string, return the length of the largest "block" in the string. A |
| 199 | + // block is a run of adjacent chars that are the same. |
| 200 | + // |
| 201 | + // |
| 202 | + // maxBlock("hoopla") → 2 |
| 203 | + // maxBlock("abbCCCddBBBxx") → 3 |
| 204 | + // maxBlock("") → 0 |
| 205 | + |
| 206 | + public int maxBlock(String str) { |
| 207 | + int mMax = 0; |
| 208 | + int mLength = str.length(); |
| 209 | + for (int i = 0; i < mLength; i++) { |
| 210 | + int mCount = 0; |
| 211 | + for (int j = i; j < mLength; j++) { |
| 212 | + if (str.charAt(i) == str.charAt(j)) { |
| 213 | + mCount++; |
| 214 | + } else { |
| 215 | + break; |
| 216 | + } |
| 217 | + } |
| 218 | + if (mCount > mMax) |
| 219 | + mMax = mCount; |
| 220 | + } |
| 221 | + return mMax; |
| 222 | + } |
| 223 | + |
| 224 | + // -------------------------------------------------------------------------------------------- |
| 225 | + |
| 226 | + // Given a string, return the sum of the numbers appearing in the string, |
| 227 | + // ignoring all other characters. A number is a series of 1 or more digit chars |
| 228 | + // in a row. (Note: Character.isDigit(char) tests if a char is one of the chars |
| 229 | + // '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.) |
| 230 | + // |
| 231 | + // |
| 232 | + // sumNumbers("abc123xyz") → 123 |
| 233 | + // sumNumbers("aa11b33") → 44 |
| 234 | + // sumNumbers("7 11") → 18 |
| 235 | + |
| 236 | + public int sumNumbers(String str) { |
| 237 | + int sum = 0; |
| 238 | + for (int i = 0; i < str.length(); i++) { |
| 239 | + if (Character.isDigit(str.charAt(i))) { |
| 240 | + int count = 0; |
| 241 | + for (int j = i; j < str.length(); j++) { |
| 242 | + if (Character.isDigit(str.charAt(j))) |
| 243 | + count++; |
| 244 | + else |
| 245 | + break; |
| 246 | + } |
| 247 | + sum += Integer.parseInt(str.substring(i, i + count)); |
| 248 | + i += count; |
| 249 | + } |
| 250 | + } |
| 251 | + return sum; |
| 252 | + } |
| 253 | + |
| 254 | + // -------------------------------------------------------------------------------------------- |
| 255 | + |
| 256 | + // Given a string, return a string where every appearance of the lowercase word |
| 257 | + // "is" has been replaced with "is not". The word "is" should not be immediately |
| 258 | + // preceeded or followed by a letter -- so for example the "is" in "this" does |
| 259 | + // not count. (Note: Character.isLetter(char) tests if a char is a letter.) |
| 260 | + // |
| 261 | + // |
| 262 | + // notReplace("is test") → "is not test" |
| 263 | + // notReplace("is-is") → "is not-is not" |
| 264 | + // notReplace("This is right") → "This is not right" |
| 265 | + |
| 266 | + public String notReplace(String str) { |
| 267 | + String result = ""; |
| 268 | + str = " " + str + " "; // avoid issues with corner cases |
| 269 | + for (int i = 0; i < str.length() - 2; i++) { |
| 270 | + if (str.charAt(i) == 'i') { |
| 271 | + if (str.charAt(i + 1) == 's' && !Character.isLetter(str.charAt(i + 2)) |
| 272 | + && !Character.isLetter(str.charAt(i - 1))) { |
| 273 | + result += "is not"; |
| 274 | + i += 1; |
| 275 | + } else |
| 276 | + result += "i"; |
| 277 | + } else |
| 278 | + result += str.charAt(i); |
| 279 | + } |
| 280 | + return result.substring(1); |
| 281 | + } |
| 282 | + |
| 283 | +} |
0 commit comments