Problem: Write a program in Java to count and output the frequency of a substring in a given string.

Example:

```Input:
Tim
Output: 3

Input:
sssss
ss
Output: 4```

To count the frequency of a substring in a given string, we match the substring with the subset of the given string using the `contain()` method.

If a match is found, we increment the counter and remove the match from the string. We repeat this process until all the matches are found.

Steps to follow to count the frequency of a substring in a string in Java:

1. Input the string and substring.
2. Check using the `contain()` method if the string contains the given string.
3. If so, then increment the counter and remove the first letter of a substring from the string using the `substring()` method.
4. Repeat steps 2 and 3 until the contain() method gives a positive result.
5. Output the value of the counter.

The `substring(start, end + 1)` is a method available in the String class that trims a string from the ‘start’ index to the ‘end’ index.

Whereas the `contain(substring)` checks the existence of the substring in the given string. If present, it returns the index of its first appearance otherwise it returns -1.

Here is the Java program that implements the above steps to count the frequency of a substring in a string:

``````import java.util.Scanner;

public class StringFrequency {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String str, match;

//enter the parent string
System.out.println("Enter the main String:");
str = in.next();

//enter the substring
System.out.println("Enter the match String:");
match = in.next();

//intialize counter as 0
int index , count =0;

//check until all matches are found
while((index = str.contain(match)) != -1){
count++;
str = str.substring(index+1);      //remove first letter of the match
}

//output the counter
System.out.println("Count: "+ count);

}
}``````

Output:

Enter the main String:
TimsdsfTimsfdTim
Enter the match String:
Tim
Count: 3

Enter the main String:
sssss
Enter the match String:
ss
Count: 4

In the above program, when a match is found in the string we remove its first letter using the `substring()` method so that it doesn’t get match again.

The reason behind removing only the first letter instead of the whole match is that a part of the match could constitute another match like ‘ss’ in ‘sssss’.

Breakdown of the Program:

```String str = "TimsdsfTimsfdTim";
String match = "Tim"

index = str.contain(match)   // returns 0 because first appearance of "Tim" start from 0 index
str = str.substring(index+1) // returns "imsdsfTimsfdTim"

index = str.contain(match)   // returns 6 because first appearance of "Tim" in updated 'str' start from 6
str = str.substring(index+1) // returns "imsfdTim"

index = str.contain(match)   // returns 5 because first appearance of "Tim" in updated 'str' start from 5
str = str.substring(index+1) // returns "im"

index = str.contain(match)   // returns -1 because "Tim" is not present in updated 'str'
```

This is just one way of counting the frequency of a substring in Java. There are lot other methods using which we can efficiently do the same task.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6814585,"math_prob":0.8799889,"size":3115,"snap":"2021-31-2021-39","text_gpt3_token_len":745,"char_repetition_ratio":0.17518483,"word_repetition_ratio":0.088932805,"special_character_ratio":0.2446228,"punctuation_ratio":0.12751678,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95366514,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-29T02:15:07Z\",\"WARC-Record-ID\":\"<urn:uuid:31eb15e8-226a-4b3b-8fe9-4df8f5c3875c>\",\"Content-Length\":\"634939\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:93993ba8-1922-479c-aeed-58feb520d680>\",\"WARC-Concurrent-To\":\"<urn:uuid:5918edcd-286f-42d0-abb8-fa90bea12ba1>\",\"WARC-IP-Address\":\"104.21.86.124\",\"WARC-Target-URI\":\"https://pencilprogrammer.com/java-programs/substring-frequency/\",\"WARC-Payload-Digest\":\"sha1:ZB3Y5NAZCYV2FS7XRIWPK76MVHZLQDFT\",\"WARC-Block-Digest\":\"sha1:XDBWKPYKWW7J33BZDPJDVV25RNCUW3O4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780061350.42_warc_CC-MAIN-20210929004757-20210929034757-00349.warc.gz\"}"}