How to Solve CodingBat Question part 2

CodingBat java String-2 Questiona and Solutions

catDog - Return true if the string "cat" and "dog" appear the same number of times in the given string.

public boolean catDog(String str) {
  int count = 0, count1 = 0, i;
  for (i = 0; i < str.length() - 2; i++) {
    if (str.substring(i, i + 3).equals("cat")) count++;
    if (str.substring(i, i + 3).equals("dog")) count1++;
  }
  if (count == count1)
    return true;
  else
    return false;
}
countCode - Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.

public int countCode(String str) {
  int count = 0;
  char[] ch = str.toCharArray();
  for (int i = 0; i < ch.length - 3; i++) {
    if (ch[i] == 'c' && ch[i + 1] == 'o' & & ch[i + 3] == 'e')
      count++;
  }
  return count;
}
endOther - Given two strings, return true if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be "case sensitive"). Note: str.toLowerCase() returns the lowercase version of a string.

public boolean endOther(String a, String b) {
  a = a.toLowerCase();
  int aLen = a.length();
  b = b.toLowerCase();
  int bLen = b.length();
  if (aLen < bLen) {
    String temp = b.substring(bLen - aLen, bLen);
    if (temp.compareTo(a) == 0)
      return true;
    else
      return false;
  } else {
    String temp = a.substring(aLen - bLen, aLen);
    if (temp.compareTo(b) == 0)
      return true;
    else
      return false;
  }
}
xyzThere - Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.

public boolean xyzThere(String str) {
  boolean m = false;
  int len = str.length();
  if (len < 3) return false;
  for (int i = 0; i < len - 2; i++) {
    if (str.substring(i, i + 3).compareTo("xyz") == 0 & & i == 0)
      m = true;
    else if (str.substring(i, i + 3).compareTo("xyz") == 0 & & str.charAt(i - 1) != '.')
      m = true;
  }
  return m;
}
bobThere - Return true if the given string contains a "bob" string, but where the middle 'o' char can be any char.

public boolean bobThere(String str) {
  char[] ch = str.toCharArray();
  for (int i = 0; i < ch.length - 2; i++) {
    if (ch[i] == 'b' & & ch[i + 2] == 'b')
      return true;
  }
  return false;
}
xyBalance - We'll say that a String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string. So "xxy" is balanced, but "xyx" is not. One 'y' can balance multiple 'x's. Return true if the given string is xy-balanced.

public boolean xyBalance(String str) {
  Boolean x = false;
  Boolean y = false;
  int len = str.length();
  for (int i = 0; i < len; i++) {
    if (str.charAt(i) == 'x' & & y == true) {
      x = true;
      y = false;
    } else if (str.charAt(i) == 'x') {
      x = true;
    }
    if (str.charAt(i) == 'y' & & x == true)
      y = true;
  }
  if (x == false)
    y = true;
  return y;
}
mixString - Given two strings, a and b, create a bigger string made of the first char of a, the first char of b, the second char of a, the second char of b, and so on. Any leftover chars go at the end of the result.

public String mixString(String a, String b) {
  int len1 = a.length(), len2 = b.length();
  int len3 = len2 + len1;
  String str = "";
  char[] s1 = a.toCharArray();
  char[] s2 = b.toCharArray();
  for (int i = 0, j = 0; i < len1 || j < len2; i++, j++) {
    if ((i < len1) & &
      (j < len2)) {
      str = str + s1[i] + s2[j];
    } else {
      if (i > = len1)
        str = str + s2[j];
      if (j > = len2)
        str = str + s1[i];
    }
  }
  return str;
}
repeatEnd - Given a string and an int n, return a string made of n repetitions of the last n characters of the string. You may assume that n is between 0 and the length of the string, inclusive.

public String repeatEnd(String str, int n) {
  String a = "";
  for (int i = 0; i < n; i++) {
    a = a + str.substring(str.length() - n, str.length());
  }
  return a;
}
repeatFront - Given a string and an int n, return a string made of the first n characters of the string, followed by the first n-1 characters of the string, and so on. You may assume that n is between 0 and the length of the string, inclusive (i.e. n >= 0 and n < str.length()).

public String repeatFront(String str, int n) {

  int len = str.length();
  String a = "";
  for (int i = n; i > 0; i--) {
    a = a + str.substring(0, n);
    n--;
  }
  return a;
}
repeatSeparator - Given two strings, word and a separator sep, return a big string made of count occurrences of the word, separated by the separator string.

public String repeatSeparator(String word, String sep, int count) {
  String a = "";
  for (int i = 1; i < = count; i++) {
    a = a + word;
    if (i < count)
      a = a + sep;
  }
  return a;
}
prefixAgain - Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().

public boolean prefixAgain(String str, int n) {
  String a = str.substring(0, n);
  String b = str.substring(n, str.length());
  if (b.contains(a))
    return true;
  else
    return false;
}
getSandwich - A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread.

public String getSandwich(String str) {
  if (str.indexOf( 'd') == -1 || str.lastIndexOf( 'b') == -1 || str.indexOf('d') > str.lastIndexOf('b'))
    return "";
  else
    return str.substring(str.indexOf('d') + 1, str.lastIndexOf('b'));
}
sameStarChar - Returns true if for every '*' (star) in the string, if there are chars both immediately before and after the star, they are the same.

public boolean sameStarChar(String str) {
  char[] ch = str.toCharArray();
  boolean f = true;
  for (int i = 0; i < str.length() - 1; i++) {
    if ((ch[i] == '*') & & i > 0) {
      if ((ch[i - 1] != ch[i + 1]))
        f = false;
      else
        f = true;
    }
  }
  return f;
}
oneTwo - Given a string, compute a new string by moving the first char to come after the next two chars, so "abc" yields "bca". Repeat this process for each subsequent group of 3 chars, so "abcdef" yields "bcaefd". Ignore any group of fewer than 3 chars at the end.

public String oneTwo(String str) {
  String s = "";
  if (str.length() < 3) return "";
  else {
    for (int i = 0; i < = str.length() - 3; i += 3)
      s = s + str.substring(i + 1, i + 3) + str.charAt(i);
  }
  return s;
}
oneTwo - Given a string, compute a new string by moving the first char to come after the next two chars, so "abc" yields "bca". Repeat this process for each subsequent group of 3 chars, so "abcdef" yields "bcaefd". Ignore any group of fewer than 3 chars at the end.

public String oneTwo(String str) {
  String s = "";
  if (str.length() < 3) return "";
  else {
    for (int i = 0; i < = str.length() - 3; i += 3)
      s = s + str.substring(i + 1, i + 3) + str.charAt(i);
  }
  return s;
}

Post a Comment

Previous Post Next Post

Recent Posts

Facebook