diff --git a/src/main/java/io/zipcoder/StringsAndThings.java b/src/main/java/io/zipcoder/StringsAndThings.java index 073467a..57bb123 100644 --- a/src/main/java/io/zipcoder/StringsAndThings.java +++ b/src/main/java/io/zipcoder/StringsAndThings.java @@ -15,7 +15,22 @@ public class StringsAndThings { * countYZ("day fyyyz"); // Should return 2 */ public Integer countYZ(String input){ - return null; + int count = 0; + String[] arrayOfWords = input.split(" "); + for (int i = 0; i < arrayOfWords.length; i++) { + String word = arrayOfWords[i]; + int numberOfCharactersInWord = word.length(); + int lastIndex = numberOfCharactersInWord -1; + char lastCharacter = word.charAt(lastIndex); + if(lastCharacter == 'y' || lastCharacter == 'z'){ + + count++; + + } + //if the last character of word is equals to y or z + + } + return count; } /** @@ -27,31 +42,68 @@ public Integer countYZ(String input){ * removeString("Hello there", "e") // Should return "Hllo thr" * removeString("Hello there", "x") // Should return "Hello there" */ + // + //nested loop + public String removeString(String base, String remove){ - return null; - } + String answer = base.replaceAll(remove, ""); + return answer; + } +// /** - * 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) + * 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 */ public Boolean containsEqualNumberOfIsAndNot(String input){ - return null; + //find the number of "is" appearances + //find the number of "not appearances + //compare number of appearances + + int countIs = 0; + int countNot = 0; + int indexOf = input.indexOf("not"); + while (indexOf >= 0){ + input = input.replaceFirst("not", " "); + indexOf = input.indexOf("not"); + countNot++; + } + indexOf = input.indexOf("is"); + + while (indexOf >= 0){ + input = input.replaceFirst("is" , " "); + indexOf = input.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. + * 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){ - return null; + + public Boolean gIsHappy(String input) { + boolean returnMe = 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') { + returnMe = false; + } + } + } + return returnMe; } @@ -62,7 +114,18 @@ public Boolean gIsHappy(String input){ * countTriple("xxxabyyyycd") // Should return 3 * countTriple("a") // Should return 0 */ + /* + * identify the number of 3x repeating characters + * have that number of 3x repeating characters add to a counter + * */ public Integer countTriple(String input){ - return null; + int counter = 0; + for (int i = 1; i < input.length() - 1; i++){ + if(input.charAt(i) == input.charAt(i - 1) && input.charAt(i) == input.charAt(i + 1)){ + + counter++; + } + } + return counter; } } 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..e8f02c1 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..556ec64 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..29d3cfe 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..27178e4 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..3bc2f6c 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..715f4d3 Binary files /dev/null and b/target/test-classes/io/zipcoder/stringsandthings/RemoveStringTest.class differ