Java Program to Count Frequency of a Substring in a String

Java Programs

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

Example:

Input: 
TimadmgfdiTimsvsTim
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 indexOf() 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 indexOf() 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 indexOf() 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 indexOf(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.nextLine();
 
        //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.indexOf(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 matched 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.indexOf(match)   // returns 0 because first appearance of "Tim" start from 0 index
str = str.substring(index+1) // returns "imsdsfTimsfdTim"

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

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

index = str.indexOf(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 of other methods using which we can efficiently do the same task.

2 thoughts on “Java Program to Count Frequency of a Substring in a String”

  1. I have gone through your code implementation and found error that in java we don’t have contain method ,May I know in which version of java you have executed this code. In java we have contains() method and that has return type as Boolean.

Leave a Reply to Adarsh Kumar Cancel reply

Your email address will not be published. Required fields are marked *