diff --git a/src/main/java/io/zipcoder/StringsAndThings.java b/src/main/java/io/zipcoder/StringsAndThings.java index 073467a..f44ae49 100644 --- a/src/main/java/io/zipcoder/StringsAndThings.java +++ b/src/main/java/io/zipcoder/StringsAndThings.java @@ -5,64 +5,114 @@ * @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 + * countYZ("day fez"); // Should return 2 + * countYZ("day fyyyz"); // Should return 2 */ - public Integer countYZ(String input){ - return null; + public Integer countYZ(String input) { + int count = 0; + String[] arrayOfWords = input.split(" "); + + for (String word : arrayOfWords){ + int numberOfCharacters = word.length(); + int lastIndex = numberOfCharacters - 1; + char lastChar = word.charAt(lastIndex); + if (lastChar == 'y' || lastChar == 'z'){ + 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". - * + *
* 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" + * removeString("Hello there", "e") // Should return "Hllo thr" + * removeString("Hello there", "x") // Should return "Hello there" */ - public String removeString(String base, String remove){ - return null; + public String removeString(String base, String remove) { + + 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) - * + *
* example : containsEqualNumberOfIsAndNot("This is not") // Should return false - * containsEqualNumberOfIsAndNot("This is notnot") // Should return true - * containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true + * containsEqualNumberOfIsAndNot("This is notnot") // Should return true + * containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true */ - public Boolean containsEqualNumberOfIsAndNot(String input){ - return null; + public Boolean containsEqualNumberOfIsAndNot(String input) { + int countIs = 0; + int countNot = 0; + int indexOf = input.indexOf("not"); + String currentString = input; + + while (indexOf >= 0) { + currentString = currentString.replaceFirst("not", ""); + indexOf = currentString.indexOf("not"); + countNot++; + } + indexOf = currentString.indexOf("is"); + while (indexOf >= 0) { + currentString = currentString.replaceFirst("is", ""); + indexOf = currentString.indexOf("is"); + countIs++; + } + 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. * example : gHappy("xxggxx") // Should return true - * gHappy("xxgxx") // Should return false - * gHappy("xxggyygxx") // Should return false + * gHappy("xxgxx") // Should return false + * gHappy("xxggyygxx") // Should return false */ - public Boolean gIsHappy(String input){ - return null; - } - /** - * 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){ - return null; - } + public Boolean gIsHappy(String input) { + boolean x = true; + for (int i = 0; i < input.length(); i++) { + if (input.charAt(i) == 'g') { + if (input.charAt(i+ 1) != 'g' && input.charAt(i - 1) != 'g') { + x = false; + } + + } + } + + return x; + } + + /** + * 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 triple = 0; + for (int i = 0; i < input.length() - 3; i++) { + if(input.charAt(i) == input.charAt(i + 1) && input.charAt(i + 1) == input.charAt(i + 2)) + triple++; + } + + + return triple; + + } + } + + diff --git a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java index 020cd3d..9c150d9 100644 --- a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java +++ b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java @@ -33,7 +33,7 @@ public void gIsHappyTest2(){ @Test public void gIsHappyTest3(){ Boolean actual = stringsAndThings.gIsHappy("xxggyygxx"); - Assert.assertTrue(actual); + Assert.assertFalse(actual); } } diff --git a/target/classes/io/zipcoder/StringsAndThings.class b/target/classes/io/zipcoder/StringsAndThings.class new file mode 100644 index 0000000..3f01e01 Binary files /dev/null and b/target/classes/io/zipcoder/StringsAndThings.class differ diff --git a/target/test-classes/io/zipcoder/stringsandthings/ContainsEqualNumberOfIsAndNotTest.class b/target/test-classes/io/zipcoder/stringsandthings/ContainsEqualNumberOfIsAndNotTest.class new file mode 100644 index 0000000..fd51ca8 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/ContainsEqualNumberOfIsAndNotTest.class differ diff --git a/target/test-classes/io/zipcoder/stringsandthings/CountTripleTest.class b/target/test-classes/io/zipcoder/stringsandthings/CountTripleTest.class new file mode 100644 index 0000000..20658c5 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/CountTripleTest.class differ diff --git a/target/test-classes/io/zipcoder/stringsandthings/CountYZTest.class b/target/test-classes/io/zipcoder/stringsandthings/CountYZTest.class new file mode 100644 index 0000000..00174b6 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/CountYZTest.class differ diff --git a/target/test-classes/io/zipcoder/stringsandthings/GIsHappyTest.class b/target/test-classes/io/zipcoder/stringsandthings/GIsHappyTest.class new file mode 100644 index 0000000..feefb49 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/GIsHappyTest.class differ diff --git a/target/test-classes/io/zipcoder/stringsandthings/RemoveStringTest.class b/target/test-classes/io/zipcoder/stringsandthings/RemoveStringTest.class new file mode 100644 index 0000000..ba429b6 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/RemoveStringTest.class differ