Java > String-3 > countYZ (CodingBat Solution)

Problem:

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. (Note: Character.isLetter(char) tests if a char is an alphabetic letter.)

countYZ("fez day") → 2
countYZ("day fez") → 2
countYZ("day fyyyz") → 2


Solution:

public int countYZ(String str) {
  int len = str.length();
  int count = 0;
  str = str.toLowerCase();
  
  for (int i = 0; i < len; i++) {
    
    if (str.charAt(i) == 'y' || str.charAt(i) == 'z') {
      if (i < len-1 && !Character.isLetter(str.charAt(i+1)))
        count++;
      else if (i == len-1)
        count++;
    }
  }
  return count;
}

10 comments:

  1. public int countYZ(String str) {
    str = str.toLowerCase();
    int c=0;
    for(int i=0; i<str.length()-1; i++){
    if((str.charAt(i)=='y' || str.charAt(i)=='z') && !Character.isLetter(str.charAt(i+1)))
    c++;
    }
    if(str.charAt(str.length()-1)=='y' || str.charAt(str.length()-1)=='z')
    c++;
    return c;
    }

    ReplyDelete
  2. My solution is very similar, except that I first check if the string ends with 'y' or 'z'. This simplifies the for loop a bit:

    public int countYZ(String str) {
    int count = 0;
    str = str.toLowerCase();

    if(str.endsWith("y") || str.endsWith("z")){
    count++;
    }

    for(int i = 0; i < str.length() - 1; i++){
    if( (str.charAt(i) == 'y' || str.charAt(i) == 'z') &&
    !(Character.isLetter(str.charAt(i+1))) ){
    count++;
    }
    }
    return count;
    }

    ReplyDelete
  3. public int countYZ(String str) {
    str = str.toLowerCase();
    String[] words = str.split("[^a-z]");

    int result = 0;

    for (int i = 0; i < words.length; i++)
    if (words[i].endsWith("y") || words[i].endsWith("z"))
    result++;


    return result;

    }

    ReplyDelete
    Replies
    1. split("[^a-z]"); can you explain, why you use ^

      Delete
  4. public int countYZ(String str) {
    int count = 0;
    for (int i = str.length() - 1; i >= 0; i--)
    {
    if ((Character.toLowerCase(str.charAt(i)) == 'y' || Character.toLowerCase(str.charAt(i)) == 'z') && (i == str.length() - 1 || !Character.isLetter(str.charAt(i + 1))))
    {
    count++;
    }
    }
    return count;
    }

    ReplyDelete
  5. you gonna lol on my dirty logic
    public int countYZ(String str) {
    str=str.toLowerCase()+" ";
    int count=0;
    for(int i=0;i<str.length()-1;i++){
    if(str.charAt(i)=='y' || str.charAt(i)=='z'){
    if(!Character.isLetter(str.charAt(i+1))){
    count++;
    }
    }
    }
    return count;
    }

    ReplyDelete
  6. public int countYZ(String str) {
    int c=0;
    int t=str.length()-1;
    str = str.toLowerCase();
    for(int i=0;i<str.length();i++){
    if(str.charAt(i)=='y'||str.charAt(i)=='z')
    {
    if(i < t && !Character.isLetter(str.charAt(i+1)))
    c++;
    else if (i == t)
    c++;
    }}return c;
    }

    ReplyDelete
  7. im doin all of this in python cuz java has more tasks, here u go if u are wonder :D
    def countYZ(s):
    s = s.lower()
    c = 0
    if s[len(s)-1] == "y" or s[len(s)-1] == "z":
    c += 1
    for i in range(len(s)):
    if not s[i].isalpha() and s[i-1] == "y":
    c += 1
    if not s[i].isalpha() and s[i-1] == "z":
    c += 1

    return c

    ReplyDelete
  8. The main solution at the top works better, but here is another logic for everyone.

    public int countYZ(String str) {
    int countYZ = 0;
    str = str.toLowerCase();
    for (int i = 0; i < str.length(); ++i) {
    boolean yz = (str.charAt(i) == 'y' || str.charAt(i) == 'z');
    boolean endWord = (i + 1 >= str.length() || !(Character.isLetter(str.charAt(i+1))));
    if (yz && endWord) countYZ++;
    }
    return countYZ;
    }

    ReplyDelete
  9. With Regex

    public int countYZ(String str) {
    return str.split("((?i)z[^a-zA-Z]|z$)|((?i)y[^a-zA-Z]|y$)", - 1).length - 1;
    }

    ReplyDelete