In reference to the problem here
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.) I wrote down the following solution.
public int countYZ(String str) { final int len = str.length(); int res = 0; if (len == 0) return 0; for (int i = 0; i < str.length(); i++) { if (str.substring(i, i + 1).equalsIgnoreCase("y") || str.substring(i, i + 1).equalsIgnoreCase("z")) if (((i < len - 1) && !(Character.isLetter(str.charAt(i + 1)))) || i == len -1) res += 1; } return res; }
Please feel free to review it, It looks a bit messy I know, is there a cleaner and easier solution to this?