String-3
Ref
/* 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.
*/
public int countTriple(String str) {
int count = 0;
for(int i = 0; i <= str.length() - 3; i++) {
if(str.charAt(i) == str.charAt(i+1) &&
str.charAt(i) == str.charAt(i+2))
count++;
}
return count;
}
/* Given a string, count the number of words ending in 'y' or 'z' -- so the
* 'y' in "heavy" and the 'z' in "fez" count, 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 letter immediately following it.
*/
public int countYZ(String str) {
if(str.length() == 0)
return 0;
int count = 0;
for(int i = 0; i <= str.length() - 2; i++) {
if((Character.toLowerCase(str.charAt(i)) == 'y' ||
Character.toLowerCase(str.charAt(i)) == 'z') &&
!Character.isLetter(str.charAt(i+1)))
count++;
}
if(Character.toLowerCase(str.charAt(str.length() - 1)) == 'y' ||
Character.toLowerCase(str.charAt(str.length() - 1)) == 'z')
count++;
return count;
}
/* 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).
*/
public boolean equalIsNot(String str) {
int is = 0;
int not = 0;
for(int i = 0; i <= str.length() - 3; i++) {
if(str.substring(i, i + 2).equals("is")) {
is++;
} else if(str.substring(i, i + 3).equals("not")) {
not++;
}
}
if(str.length() >= 2 && str.substring(str.length() - 2).equals("is"))
is++;
return is == not;
}
/* 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.
*/
public boolean gHappy(String str) {
if(str.length() == 1 && str.charAt(0) == 'g')
return false;
if(str.length() >= 2 &&
(str.charAt(0) == 'g' && str.charAt(1) != 'g' ||
str.charAt(str.length()-1) == 'g' &&
str.charAt(str.length()-2) != 'g'))
return false;
for(int i = 1; i <= str.length() - 2; i++) {
if(str.charAt(i) == 'g' && str.charAt(i-1) != 'g' &&
str.charAt(i+1) != 'g')
return false;
}
return true;
}
/* Given a string, return the length of the largest "block" in the string.
* A block is a run of adjacent chars that are the same.
*/
public int maxBlock(String str) {
if(str.length() == 0)
return 0;
int largest = 0;
int current = 1;
for(int i = 1; i < str.length(); i++) {
if(str.charAt(i) != str.charAt(i-1)) {
if(current > largest)
largest = current;
current = 1;
} else {
current++;
}
}
return Math.max(largest, current);
}
/* Given a string, look for a mirror image (backwards) string at both the
* beginning and end of the given string. In other words, zero or more
* characters at the very begining of the given string, and at the very end
* of the string in reverse order (possibly overlapping). For example, the
* string "abXYZba" has the mirror end "ab".
*/
public String mirrorEnds(String string) {
StringBuilder result = new StringBuilder();
for(int i = 0; i < string.length(); i++) {
if(string.charAt(i) == string.charAt(string.length() - i - 1))
result.append(string.charAt(i));
else
break;
}
return result.toString();
}
/* Given a string, return a string where every appearance of the lowercase
* word "is" has been replaced with "is not". The word "is" should not be
* immediately preceeded or followed by a letter -- so for example the "is"
* in "this" does not count.
*/
public String notReplace(String str) {
if(str.equals("is"))
return "is not";
StringBuilder result = new StringBuilder();
int i = 0;
if(str.length() >= 3 && str.substring(0,2).equals("is") &&
!Character.isLetter(str.charAt(2))) {
result.append("is not");
i = 2;
}
while(i < str.length()) {
if(!Character.isLetter(str.charAt(i))) {
result.append(str.charAt(i));
i++;
} else if(i >= 1 && i <= str.length()-3 &&
!Character.isLetter(str.charAt(i-1)) &&
str.substring(i,i+2).equals("is") &&
!Character.isLetter(str.charAt(i+2))) {
result.append("is not");
i += 2;
} else if(i >= 1 && !Character.isLetter(str.charAt(i-1)) &&
str.substring(i).equals("is")) {
result.append("is not");
i += 2;
} else {
result.append(str.charAt(i));
i++;
}
}
return result.toString();
}
/* Given a string, return the longest substring that appears at both the
* beginning and end of the string without overlapping. For example,
* sameEnds("abXab") is "ab".
*/
public String sameEnds(String string) {
int start = (int) Math.ceil((double) string.length() / 2);
int end = string.length() / 2;
for(int i = 0; i < string.length() / 2; i++) {
if(string.substring(0, end).equals(string.substring(start))) {
return string.substring(0, end);
} else {
start++;
end--;
}
}
return "";
}
/* Given a string, return the sum of the digits 0-9 that appear in the
* string, ignoring all other characters. Return 0 if there are no digits in
* the string.
*/
public int sumDigits(String str) {
int sum = 0;
for(int i = 0; i < str.length(); i++) {
if(Character.isDigit(str.charAt(i)))
sum = sum + str.charAt(i) - '0';
}
return sum;
}
/* Given a string, return the sum of the numbers appearing in the string,
* ignoring all other characters. A number is a series of 1 or more digit
* chars in a row.
*/
public int sumNumbers(String str) {
int sum = 0;
int i = 0;
int begin;
int end;
while(i < str.length() && !Character.isDigit(str.charAt(i)))
i++;
begin = i;
end = i;
while(i < str.length()) {
if(!Character.isDigit(str.charAt(i))) {
sum += Integer.parseInt(str.substring(begin, end));
while(i < str.length() && !Character.isDigit(str.charAt(i)))
i++;
begin = i;
end = i;
} else {
end++;
i++;
}
}
if(end > begin)
sum += Integer.parseInt(str.substring(begin, end));
return sum;
}
/* 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.
* Remove only non-overlapping instances, so with "xxx" removing "xx"
* leaves "x".
*/
public String withoutString(String base, String remove) {
char[] arr = new char[base.length()];
int count = 0;
int i = 0;
while(i <= base.length() - remove.length()) {
if(base.substring(i, i + remove.length()).toLowerCase().equals(
remove.toLowerCase())) {
i += remove.length();
} else {
arr[count] = base.charAt(i);
count++;
i++;
}
}
while(i < base.length()) {
arr[count] = base.charAt(i);
count++;
i++;
}
return new String(arr, 0, count);
}
Last updated