how to count the number of occurrences of a substring in a string in java in o(1) code example
Example: find number of occurrences of a substring in a string java
public static int count(String str, String target) {
return (str.length() - str.replace(target, "").length()) / target.length();
}