
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Count occurrences of a substring recursively in Java
Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive process.
A recursive function is the one which has its own call inside it’s definition.
If str1 is “I know that you know that i know” str2=”know”
Count of occurences is − 3
Let us understand with examples.
For Example
Input
str1 = "TPisTPareTPamTP", str2 = "TP";
Output
Count of occurrences of a substring recursively are: 4
Explanation
The substring TP occurs 4 times in str1.
Input
str1 = "HiHOwAReyouHiHi" str2 = "Hi"
Output
Count of occurrences of a substring recursively are: 3
Explanation
The substring Hi occurs 3 times in str1.
Approach used in the below program is as follows −
In this approach we will search for occurrence of str2 in str1 using contains() method in java. It will return true if str2 exists in str1. In case true, remove that first occurrence from str1 by replacing it with “” using replaceFirst() method in java and add 1 to return value to increase count.
Take two strings as str1 and str2.
Recursive Method subsrting_rec(String str, String sub) takes string str and its substring sub and returns the count of occurrences of sub in str.
Check whether str.contains(sub) is true. ( str has sub )
If true then replace the first occurrence of sub with “” using str.replaceFirst(sub,””).
Do this inside a recursive call to subsrting_rec(String str, String sub).
At the end of all recursions the sum of all return values is count.
Print the result.
Example
public class recursive{ public static void main(String args[]){ String str1 = "TPisTPareTPamTP", str2 = "TP"; System.out.println("Count of occurrences of a substring recursively are: "+subsrting_rec(str1, str2)); } static int subsrting_rec(String str, String sub){ if (str.contains(sub)){ return 1 + subsrting_rec(str.replaceFirst(sub, ""), sub); } return 0; } }
Output
If we run the above code it will generate the following output −
Count of occurrences of a substring recursively are: 4
- Related Articles
- Java program to count occurrences of a word in string
- Maximum Number of Occurrences of a Substring in C++
- Java program to count the occurrences of each character
- Count Occurrences of Anagrams in C++
- Count occurrences of a char in a text file
- Count occurrences of a character in string in Python
- Replace All Occurrences of a Python Substring with a New String?
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- Count occurrences of an element in a list in Python
- Count occurrences of a character in a repeated string in C++
- C# program to count occurrences of a word in string
- Python – All occurrences of Substring from the list of strings
- Python program to count occurrences of a word in a string
- Count multiple occurrences of separate texts in MySQL?
- Count occurrences of known distinct values in MySQL
