diff --git a/src/main/java/io/zipcoder/StringsAndThings.java b/src/main/java/io/zipcoder/StringsAndThings.java index 073467a..6d3a45a 100644 --- a/src/main/java/io/zipcoder/StringsAndThings.java +++ b/src/main/java/io/zipcoder/StringsAndThings.java @@ -14,10 +14,23 @@ public class StringsAndThings { * 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; //counts the number of times the word ends in a 'y' or 'z' + String[] numberOfWords = input.split(" "); // this splits where there is a space + for (String word : numberOfWords) { + int charactersInWord = word.length(); //this is checking the length of characters in a word + char lastChar = word.charAt(charactersInWord - 1); // we are looking for last character, because we count starting + // at zero, we have to subtract 1 for it to give us the + // correct last character + if (lastChar == 'y' || lastChar == 'z') { //this is saying if the last character is either y or z then it will + // return our desired return + 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. @@ -27,10 +40,15 @@ public Integer countYZ(String input){ * 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, ""); //remove the characters that were passed through 'remove' + // because it replaces the characters with "" which is an empty string } + + + /** * 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) @@ -39,10 +57,29 @@ public String removeString(String base, String remove){ * containsEqualNumberOfIsAndNot("This is notnot") // Should return true * containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true */ + + public Boolean containsEqualNumberOfIsAndNot(String input){ - return null; + int is = 0; + int not = 0; + + for (int i =0; i <= input.length() - 3; i++) { // this is initiating at the beginning and running until 3 less than + // the length of the String "input" + if (input.startsWith("is", i)) { // IntelliJ recommended that I use the .startsWith instead of a substring. + // This allows it to check for "is" + is++; // This is allowing it to go to the next "is" + } else if (input.startsWith("not", i)) { // Similar to line 65 except I have "else if" because it is + // saying if it isn't "is", then look for "not" + not++; // Continues to look for other "not"s + } + } + + return is == not; // Finally we compare "is" and "not", this will check to see + // if the appearances are equal } + + /** * 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. @@ -50,11 +87,26 @@ public Boolean containsEqualNumberOfIsAndNot(String input){ * gHappy("xxgxx") // Should return false * gHappy("xxggyygxx") // Should return false */ - public Boolean gIsHappy(String input){ - return null; + // g must be lowercase + // g is only happy when directly next to another g + // all g's in the string must be happy for it to return true + // probably need to use char to detect a specific character, need lowercase to make sure it isn't a Gg or gG scenario + public Boolean gIsHappy(String input) { + for (int i = 0; i < input.length(); i++) { // the iterates and increments through + if (input.charAt(i) == 'g' && input.charAt(i + 1) == 'g') { // this says that I have a character 'g' and + // if there exists another 'g' next to that g + // then gishappy and will return true. + // (i + 1) just makes sure that the g's are next to each other. + // We didn't need to add an (i-1) + // because the left right part doesn't matter + // because when it finds 'g' it will read from + // left to right, so that first 'g' will always be the left g. + return true; + } + } + return false; } - /** * 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. @@ -62,7 +114,15 @@ public Boolean gIsHappy(String input){ * countTriple("xxxabyyyycd") // Should return 3 * countTriple("a") // Should return 0 */ - public Integer countTriple(String input){ - return null; + public Integer countTriple(String input){ + int count = 0; //counts number of times + + for (int i = 0; i <= input.length() - 3; i++) { // the - 3 is there because we are looking for a three character string inside of a larger string + if (input.charAt(i) == input.charAt(i + 1) && input.charAt(i) == input.charAt(i+2)) + count++; + + } + return count; } + } diff --git a/src/test/java/io/zipcoder/stringsandthings/CountYZTest.java b/src/test/java/io/zipcoder/stringsandthings/CountYZTest.java index 35538ef..44bd40b 100644 --- a/src/test/java/io/zipcoder/stringsandthings/CountYZTest.java +++ b/src/test/java/io/zipcoder/stringsandthings/CountYZTest.java @@ -25,6 +25,7 @@ public void countYZTest1(){ Assert.assertEquals(expected, actual); } + @Test public void countYZTest2(){ String input = "day fez"; diff --git a/target/classes/io/zipcoder/StringsAndThings.class b/target/classes/io/zipcoder/StringsAndThings.class new file mode 100644 index 0000000..b353a52 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..5f6c933 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..97d69c0 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