alizalang 6 лет назад
Родитель
Сommit
e200732e57

+ 81
- 24
src/main/java/io/zipcoder/StringsAndThings.java Просмотреть файл

@@ -2,63 +2,120 @@ package io.zipcoder;
2 2
 
3 3
 public class StringsAndThings {
4 4
 
5
+
5 6
     /**
6 7
      * Given a string, count the number of words ending in 'y' or 'z' -- so the 'y' in "heavy" and the 'z' in "fez" count,
7 8
      * 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
8 9
      * letter immediately following it. (Note: Character.isLetter(char) tests if a char is an alphabetic letter.)
9 10
      * example : countYZ("fez day"); // Should return 2
10
-     *           countYZ("day fez"); // Should return 2
11
-     *           countYZ("day fyyyz"); // Should return 2
11
+     * countYZ("day fez"); // Should return 2
12
+     * countYZ("day fyyyz"); // Should return 2
12 13
      */
13
-    public Integer countYZ(String input){
14
-        return null;
14
+    public Integer countYZ(String input) {
15
+
16
+        input.toLowerCase();
17
+        String[] inputArray = input.split(" ");
18
+
19
+        int count = 0;
20
+
21
+        for (String eachElementInInputArray : inputArray)
22
+
23
+            if (eachElementInInputArray.charAt(eachElementInInputArray.length() - 1) == 'y' || (eachElementInInputArray.charAt(eachElementInInputArray.length() - 1) == 'z')) {
24
+                count += 1;
25
+            }
26
+        return count;
27
+
15 28
     }
16 29
 
30
+
17 31
     /**
18 32
      * Given two strings, base and remove, return a version of the base string where all instances of the remove string have
19 33
      * been removed (not case sensitive). You may assume that the remove string is length 1 or more.
20 34
      * Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x".
21
-     *
35
+     * <p>
22 36
      * example : withoutString("Hello there", "llo") // Should return "He there"
23
-     *           withoutString("Hello there", "e") //  Should return "Hllo thr"
24
-     *           withoutString("Hello there", "x") // Should return "Hello there"
37
+     * withoutString("Hello there", "e") //  Should return "Hllo thr"
38
+     * withoutString("Hello there", "x") // Should return "Hello there"
25 39
      */
26
-    public String withoutString(String base, String remove){
27
-        return null;
40
+    public String withoutString(String base, String remove) {
41
+
42
+        remove.toLowerCase();
43
+        String str = base.replaceAll(remove, "");
44
+        return str;
28 45
     }
29 46
 
30 47
     /**
31 48
      * Given a string, return true if the number of appearances of "is" anywhere in the string is equal
32 49
      * to the number of appearances of "not" anywhere in the string (case sensitive)
33
-     *
50
+     * <p>
34 51
      * example : equalIsNot("This is not")  // Should return false
35
-     *           equalIsNot("This is notnot") // Should return true
36
-     *           equalIsNot("noisxxnotyynotxisi") // Should return true
52
+     * equalIsNot("This is notnot") // Should return true
53
+     * equalIsNot("noisxxnotyynotxisi") // Should return true
37 54
      */
38
-    public Boolean equalIsNot(String input){
39
-        return null;
55
+
56
+
57
+    public Boolean equalIsNot(String input) {
58
+
59
+
60
+        int isCount = 0;
61
+        int notCount = 0;
62
+
63
+
64
+        for (int i = 0; i < input.length() - 1; i++) {
65
+            if ((input.charAt(i) == 'i') && (input.charAt(i + 1) == 's')) {
66
+                isCount += 1;
67
+            }
68
+        }
69
+
70
+        for (int i = 0; i < input.length() - 2; i++) {
71
+            if ((input.charAt(i) == 'n') && (input.charAt(i + 1) == 'o') && (input.charAt(i + 2) == 't')) {
72
+                notCount += 1;
73
+            }
74
+        }
75
+
76
+        if (isCount == notCount)
77
+            return true;
78
+        else
79
+            return false;
80
+
40 81
     }
41 82
 
42 83
     /**
43 84
      * We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right.
44 85
      * Return true if all the g's in the given string are happy.
45 86
      * example : gHappy("xxggxx") // Should return  true
46
-     *           gHappy("xxgxx") // Should return  false
47
-     *           gHappy("xxggyygxx") // Should return  false
87
+     * gHappy("xxgxx") // Should return  false
88
+     * gHappy("xxggyygxx") // Should return  false
48 89
      */
49
-    public Boolean gIsHappy(String input){
50
-        return null;
51
-    }
90
+    public Boolean gIsHappy(String input) {
91
+
92
+        boolean flag = true;
52 93
 
94
+        for (int i = 0; i < input.length(); i++) {
95
+            if (input.charAt(i) == 'g') {
96
+                if ((input.charAt(i - 1) != 'g') && (input.charAt(i + 1) != 'g')) {
97
+                    flag = false;
98
+                }
99
+            }
100
+        }
101
+
102
+        return flag;
103
+    }
53 104
 
54 105
     /**
55 106
      * We'll say that a "triple" in a string is a char appearing three times in a row.
56 107
      * Return the number of triples in the given string. The triples may overlap.
57 108
      * example :  countTriple("abcXXXabc") // Should return 1
58
-     *            countTriple("xxxabyyyycd") // Should return 3
59
-     *            countTriple("a") // Should return 0
109
+     * countTriple("xxxabyyyycd") // Should return 3
110
+     * countTriple("a") // Should return 0
60 111
      */
61
-    public Integer countTriple(String input){
62
-        return null;
112
+    public Integer countTriple(String input) {
113
+        int count = 0;
114
+        for (int i = 0; i < input.length() - 2; i++) {
115
+            if ((input.charAt(i + 1) == input.charAt(i)) && input.charAt(i + 2) == input.charAt(i))
116
+                count += 1;
117
+        }
118
+        return count;
119
+
63 120
     }
64
-}
121
+}

+ 1
- 1
src/test/java/io/zipcoder/StringsAndThingsTest.java Просмотреть файл

@@ -92,7 +92,7 @@ public class StringsAndThingsTest {
92 92
     @Test
93 93
     public void gIsHappyTest3(){
94 94
         Boolean actual = stringsAndThings.gIsHappy("xxggyygxx");
95
-        Assert.assertTrue(actual);
95
+        Assert.assertFalse(actual);
96 96
     }
97 97
 
98 98
     @Test

Двоичные данные
target/classes/io/zipcoder/StringsAndThings.class Просмотреть файл


Двоичные данные
target/test-classes/io/zipcoder/StringsAndThingsTest.class Просмотреть файл