public static boolean checkOccurrences(String str) { int aIndexOfLastOccurrence = str.lastIndexOf('a'); int bIndexOfFirstOccurrence = str.indexOf('b'); return aIndexOfLastOccurrence == -1 || bIndexOfFirstOccurrence == -1 || aIndexOfLastOccurrence < bIndexOfFirstOccurrence; }