diff --git a/src/main/java/io/zipcoder/StringsAndThings.java b/src/main/java/io/zipcoder/StringsAndThings.java index 073467a..f8ea29f 100644 --- a/src/main/java/io/zipcoder/StringsAndThings.java +++ b/src/main/java/io/zipcoder/StringsAndThings.java @@ -15,7 +15,14 @@ public class StringsAndThings { * countYZ("day fyyyz"); // Should return 2 */ public Integer countYZ(String input){ - return null; + int countOfYZ = 0; + String[] inputToArrayOfWords = input.split(" "); + for (String word : inputToArrayOfWords) { + if (word.endsWith("y") || word.endsWith("z")) { + countOfYZ += 1; + } + } + return countOfYZ; } /** @@ -28,7 +35,8 @@ public Integer countYZ(String input){ * removeString("Hello there", "x") // Should return "Hello there" */ public String removeString(String base, String remove){ - return null; + String newString = base.replaceAll(remove, ""); + return newString; } /** @@ -40,7 +48,28 @@ public String removeString(String base, String remove){ * containsEqualNumberOfIsAndNot("noisxxnotyynotxisi") // Should return true */ public Boolean containsEqualNumberOfIsAndNot(String input){ - return null; + boolean isAppearsSameNumberOfTimesAsNot = true; + int isCount = 0; + int notCount = 0; + int numberOfIsIndex = 0; + int numberOfNotIndex = 0; + int numberOfIs = 0; + int numberOfNot = 0; + while ((numberOfIsIndex = input.indexOf("is", isCount)) != -1) { + isCount = numberOfIsIndex + 2; + numberOfIs++; + } + while ((numberOfNotIndex = input.indexOf("not", notCount)) != -1) { + notCount = numberOfNotIndex + 2; + numberOfNot++; + } + if (numberOfIs == numberOfNot) { + isAppearsSameNumberOfTimesAsNot = true; + } + else { + isAppearsSameNumberOfTimesAsNot = false; + } + return isAppearsSameNumberOfTimesAsNot; } /** @@ -51,7 +80,20 @@ public Boolean containsEqualNumberOfIsAndNot(String input){ * gHappy("xxggyygxx") // Should return false */ public Boolean gIsHappy(String input){ - return null; + boolean gIsHappy = false; + char[] inputToCharArray = input.toCharArray(); + for (int i = 1; i < inputToCharArray.length - 1; i++) { + if (inputToCharArray[i] == 'g' && (inputToCharArray[i - 1] == 'g' || inputToCharArray[i + 1] == 'g')) { + gIsHappy = true; + } + else if (inputToCharArray[i] == 'g' && (inputToCharArray[i - 1] != 'g' || inputToCharArray[i + 1] != 'g')) { + gIsHappy = false; + } + else if (inputToCharArray[0] == 'g' && inputToCharArray[1] == 'g') { + gIsHappy = true; + } + } + return gIsHappy; } @@ -62,7 +104,14 @@ 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 numberOfTriples = 0; + char[] inputToArray = input.toCharArray(); + for (int i = 0; i < inputToArray.length - 2; i++) { + if (inputToArray[i] == inputToArray[i + 1] && inputToArray[i] == inputToArray[i + 2]) { + numberOfTriples += 1; + } + } + return numberOfTriples; } } diff --git a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java index 020cd3d..29ba65c 100644 --- a/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java +++ b/src/test/java/io/zipcoder/stringsandthings/GIsHappyTest.java @@ -33,7 +33,19 @@ public void gIsHappyTest2(){ @Test public void gIsHappyTest3(){ Boolean actual = stringsAndThings.gIsHappy("xxggyygxx"); + Assert.assertFalse(actual); + } + + @Test + public void gIsHappyTest4(){ + Boolean actual = stringsAndThings.gIsHappy("ggxggyyxxgg"); Assert.assertTrue(actual); } + @Test + public void gIsHappyTest5(){ + Boolean actual = stringsAndThings.gIsHappy("gxxyyxxg"); + 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..37ea7f6 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..ca313ec 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