File tree Expand file tree Collapse file tree 11 files changed +261
-0
lines changed Expand file tree Collapse file tree 11 files changed +261
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* We'll say that a "triple" in a string is a char appearing three times in a
2
+ * row. Return the number of triples in the given string. The triples may
3
+ * overlap.
4
+ */
5
+ public int countTriple (String str ) {
6
+ int count = 0 ;
7
+
8
+ for (int i = 0 ; i <= str .length () - 3 ; i ++) {
9
+ if (str .charAt (i ) == str .charAt (i +1 ) &&
10
+ str .charAt (i ) == str .charAt (i +2 ))
11
+ count ++;
12
+ }
13
+
14
+ return count ;
15
+ }
Original file line number Diff line number Diff line change
1
+ /* Given a string, count the number of words ending in 'y' or 'z' -- so the
2
+ * 'y' in "heavy" and the 'z' in "fez" count, but not the 'y' in "yellow"
3
+ * (not case sensitive). We'll say that a y or z is at the end of a word if
4
+ * there is not an alphabetic letter immediately following it.
5
+ */
6
+ public int countYZ (String str ) {
7
+ if (str .length () == 0 )
8
+ return 0 ;
9
+
10
+ int count = 0 ;
11
+
12
+ for (int i = 0 ; i <= str .length () - 2 ; i ++) {
13
+ if ((Character .toLowerCase (str .charAt (i )) == 'y' ||
14
+ Character .toLowerCase (str .charAt (i )) == 'z' ) &&
15
+ !Character .isLetter (str .charAt (i +1 )))
16
+ count ++;
17
+ }
18
+
19
+ if (Character .toLowerCase (str .charAt (str .length () - 1 )) == 'y' ||
20
+ Character .toLowerCase (str .charAt (str .length () - 1 )) == 'z' )
21
+ count ++;
22
+
23
+ return count ;
24
+ }
Original file line number Diff line number Diff line change
1
+ /* Given a string, return true if the number of appearances of "is" anywhere
2
+ * in the string is equal to the number of appearances of "not" anywhere in
3
+ * the string (case sensitive).
4
+ */
5
+ public boolean equalIsNot (String str ) {
6
+ int is = 0 ;
7
+ int not = 0 ;
8
+
9
+ for (int i = 0 ; i <= str .length () - 3 ; i ++) {
10
+ if (str .substring (i , i + 2 ).equals ("is" )) {
11
+ is ++;
12
+ } else if (str .substring (i , i + 3 ).equals ("not" )) {
13
+ not ++;
14
+ }
15
+ }
16
+
17
+ if (str .length () >= 2 && str .substring (str .length () - 2 ).equals ("is" ))
18
+ is ++;
19
+
20
+ return is == not ;
21
+ }
Original file line number Diff line number Diff line change
1
+ /* We'll say that a lowercase 'g' in a string is "happy" if there is another
2
+ * 'g' immediately to its left or right. Return true if all the g's in the
3
+ * given string are happy.
4
+ */
5
+ public boolean gHappy (String str ) {
6
+ if (str .length () == 1 && str .charAt (0 ) == 'g' )
7
+ return false ;
8
+
9
+ if (str .length () >= 2 &&
10
+ (str .charAt (0 ) == 'g' && str .charAt (1 ) != 'g' ||
11
+ str .charAt (str .length ()-1 ) == 'g' &&
12
+ str .charAt (str .length ()-2 ) != 'g' ))
13
+ return false ;
14
+
15
+ for (int i = 1 ; i <= str .length () - 2 ; i ++) {
16
+ if (str .charAt (i ) == 'g' && str .charAt (i -1 ) != 'g' &&
17
+ str .charAt (i +1 ) != 'g' )
18
+ return false ;
19
+ }
20
+
21
+ return true ;
22
+ }
23
+
Original file line number Diff line number Diff line change
1
+ /* Given a string, return the length of the largest "block" in the string.
2
+ * A block is a run of adjacent chars that are the same.
3
+ */
4
+ public int maxBlock (String str ) {
5
+ if (str .length () == 0 )
6
+ return 0 ;
7
+
8
+ int largest = 0 ;
9
+ int current = 1 ;
10
+
11
+ for (int i = 1 ; i < str .length (); i ++) {
12
+ if (str .charAt (i ) != str .charAt (i -1 )) {
13
+ if (current > largest )
14
+ largest = current ;
15
+ current = 1 ;
16
+ } else {
17
+ current ++;
18
+ }
19
+ }
20
+
21
+ return Math .max (largest , current );
22
+ }
Original file line number Diff line number Diff line change
1
+ /* Given a string, look for a mirror image (backwards) string at both the
2
+ * beginning and end of the given string. In other words, zero or more
3
+ * characters at the very begining of the given string, and at the very end
4
+ * of the string in reverse order (possibly overlapping). For example, the
5
+ * string "abXYZba" has the mirror end "ab".
6
+ */
7
+ public String mirrorEnds (String string ) {
8
+ StringBuilder result = new StringBuilder ();
9
+
10
+ for (int i = 0 ; i < string .length (); i ++) {
11
+ if (string .charAt (i ) == string .charAt (string .length () - i - 1 ))
12
+ result .append (string .charAt (i ));
13
+ else
14
+ break ;
15
+ }
16
+
17
+ return result .toString ();
18
+ }
Original file line number Diff line number Diff line change
1
+ /* Given a string, return a string where every appearance of the lowercase
2
+ * word "is" has been replaced with "is not". The word "is" should not be
3
+ * immediately preceeded or followed by a letter -- so for example the "is"
4
+ * in "this" does not count.
5
+ */
6
+ public String notReplace (String str ) {
7
+ if (str .equals ("is" ))
8
+ return "is not" ;
9
+
10
+ StringBuilder result = new StringBuilder ();
11
+ int i = 0 ;
12
+
13
+ if (str .length () >= 3 && str .substring (0 ,2 ).equals ("is" ) &&
14
+ !Character .isLetter (str .charAt (2 ))) {
15
+ result .append ("is not" );
16
+ i = 2 ;
17
+ }
18
+
19
+ while (i < str .length ()) {
20
+ if (!Character .isLetter (str .charAt (i ))) {
21
+ result .append (str .charAt (i ));
22
+ i ++;
23
+ } else if (i >= 1 && i <= str .length ()-3 &&
24
+ !Character .isLetter (str .charAt (i -1 )) &&
25
+ str .substring (i ,i +2 ).equals ("is" ) &&
26
+ !Character .isLetter (str .charAt (i +2 ))) {
27
+ result .append ("is not" );
28
+ i += 2 ;
29
+ } else if (i >= 1 && !Character .isLetter (str .charAt (i -1 )) &&
30
+ str .substring (i ).equals ("is" )) {
31
+ result .append ("is not" );
32
+ i += 2 ;
33
+ } else {
34
+ result .append (str .charAt (i ));
35
+ i ++;
36
+ }
37
+ }
38
+
39
+ return result .toString ();
40
+ }
Original file line number Diff line number Diff line change
1
+ /* Given a string, return the longest substring that appears at both the
2
+ * beginning and end of the string without overlapping. For example,
3
+ * sameEnds("abXab") is "ab".
4
+ */
5
+ public String sameEnds (String string ) {
6
+ int start = (int ) Math .ceil ((double ) string .length () / 2 );
7
+ int end = string .length () / 2 ;
8
+
9
+ for (int i = 0 ; i < string .length () / 2 ; i ++) {
10
+ if (string .substring (0 , end ).equals (string .substring (start ))) {
11
+ return string .substring (0 , end );
12
+ } else {
13
+ start ++;
14
+ end --;
15
+ }
16
+ }
17
+
18
+ return "" ;
19
+ }
Original file line number Diff line number Diff line change
1
+ /* Given a string, return the sum of the digits 0-9 that appear in the
2
+ * string, ignoring all other characters. Return 0 if there are no digits in
3
+ * the string.
4
+ */
5
+ public int sumDigits (String str ) {
6
+ int sum = 0 ;
7
+
8
+ for (int i = 0 ; i < str .length (); i ++) {
9
+ if (Character .isDigit (str .charAt (i )))
10
+ sum = sum + str .charAt (i ) - '0' ;
11
+ }
12
+
13
+ return sum ;
14
+ }
Original file line number Diff line number Diff line change
1
+ /* Given a string, return the sum of the numbers appearing in the string,
2
+ * ignoring all other characters. A number is a series of 1 or more digit
3
+ * chars in a row.
4
+ */
5
+ public int sumNumbers (String str ) {
6
+ int sum = 0 ;
7
+ int i = 0 ;
8
+ int begin ;
9
+ int end ;
10
+
11
+ while (i < str .length () && !Character .isDigit (str .charAt (i )))
12
+ i ++;
13
+
14
+ begin = i ;
15
+ end = i ;
16
+
17
+ while (i < str .length ()) {
18
+ if (!Character .isDigit (str .charAt (i ))) {
19
+ sum += Integer .parseInt (str .substring (begin , end ));
20
+ while (i < str .length () && !Character .isDigit (str .charAt (i )))
21
+ i ++;
22
+
23
+ begin = i ;
24
+ end = i ;
25
+ } else {
26
+ end ++;
27
+ i ++;
28
+ }
29
+ }
30
+
31
+ if (end > begin )
32
+ sum += Integer .parseInt (str .substring (begin , end ));
33
+
34
+ return sum ;
35
+ }
Original file line number Diff line number Diff line change
1
+ /* Given two strings, base and remove, return a version of the base string
2
+ * where all instances of the remove string have been removed (not case
3
+ * sensitive). You may assume that the remove string is length 1 or more.
4
+ * Remove only non-overlapping instances, so with "xxx" removing "xx"
5
+ * leaves "x".
6
+ */
7
+ public String withoutString (String base , String remove ) {
8
+ char [] arr = new char [base .length ()];
9
+ int count = 0 ;
10
+ int i = 0 ;
11
+
12
+ while (i <= base .length () - remove .length ()) {
13
+ if (base .substring (i , i + remove .length ()).toLowerCase ().equals (
14
+ remove .toLowerCase ())) {
15
+ i += remove .length ();
16
+ } else {
17
+ arr [count ] = base .charAt (i );
18
+ count ++;
19
+ i ++;
20
+ }
21
+ }
22
+
23
+ while (i < base .length ()) {
24
+ arr [count ] = base .charAt (i );
25
+ count ++;
26
+ i ++;
27
+ }
28
+
29
+ return new String (arr , 0 , count );
30
+ }
You can’t perform that action at this time.
0 commit comments