Java Program to find occurrence of a pattern in String with (and without) String methods

6:21 PM 4 Comments A+ a-

Finding patterns in the String : The problem description

Today, I will be writing a program to print the occurrence of a String pattern in the given input String in two ways: one using the String substring() method (which will make the task very easy) and second without using the substring() method (similar but need to do the substring calculation and comparison manually).
The scenario is we have an input String of characters and we want to find the occurrences of some pattern (which will of course be the substring of the input string). 
For example:- 
Input String: "CODEINJAVATUTORIALSFORJAVABEGINNERS"
Patter String: "JAVA"
Occurrence: 2

Logic for finding the pattern occurrences in the String

We will go for the simple and straightforward logic. And that is, we will be finding all the sub-strings of length equal to the length of the pattern we are looking for. And then from those list of sub-strings, we will pick sub-strings one by one and compare it against the patter string. Whenever the match happens, we will increment the occurrence count. So, the main task here seems to be just finding all the sub-strings of length L equal to the length of pattern string. The rest part is just comparison with the pattern and counting the occurrences. 

Using String substring() method

Using the String utility method substring() for finding the sub-strings makes this task extremely easy and straightforward. Below is the method that takes the input string and pattern string and return the number of occurrences of pattern in input string. 
public int countOccurenceOfPattern(String input, String pattern) {
  int inputLength = input.length();
  int patternLength = pattern.length();
  int occurrence = 0;

  // Using String methods. In this case we'll need only substring()
  for (int i = 0; i < (inputLength - patternLength); i++) {
   if ((input.substring(i, i + patternLength)).equals(pattern))
    occurrence++;
  }

  return occurrence;
 }

Without using the substring() method

Well, here is when we will have to do a bit of logic building and do the things manually. Well this doesn't complicate the code much and is still very straightforward. We will be creating the substrings of length L equal to length of pattern manually iterating through the two loops and comparing the each substring with the pattern string. Below is the previous method re-written to achieve the same objective without using the substring() method.
public int countOccurenceOfPattern(String input, String pattern) {
  int inputLength = input.length();
  int patternLength = pattern.length();
  int occurrence = 0;

  // Without using substring()
  // In that we will be required to do a bit of logic building ourself.
  String temp = "";

  for (int i = 0; i < (inputLength - patternLength); i++) {
   temp = "";
   for (int j = i; j < (i + patternLength); j++) {
    temp = temp + Character.toString(input.charAt(j));
   }
   if (temp.equals(pattern))
    occurrence++;
  }

  return occurrence;
 }

Currently working at Symantec in Chennai. Previously worked as a Senior Software Engineer at IGATE. Loves to explore new things, loves to code and solve challenging problems with the help of it. Like any other normal person, loves to watch movies, listen to songs (specially when travelling, it is must), getting to know people, travelling and whatever makes me feel good!

4 comments

Write comments
happy
AUTHOR
May 14, 2016 at 4:33 PM delete This comment has been removed by the author.
avatar
happy
AUTHOR
May 14, 2016 at 4:35 PM delete

in the first loop one = is required in the middle condition.if the pattern is present at last of the String the above code will not recognized.
for (int i = 0; i = < (inputLength - patternLength); i++)

Reply
avatar
ERP
AUTHOR
April 5, 2017 at 9:59 AM delete


ERPTREE Offering Oracle fusion HCM online training, Oracle Fusion SCM Online Training, Oracle fusion financials online
training, Oracle fusion hcm training, Oracle fusion scm training, Oracle fusion financials training,
Oracle fusion dba online training in Hyderabad, Bangalore,Gurgon, Noida, India, Dubai, UAE, USA, Kuwait, UK, Singapore, Saudi Arabia, Canada,
Delhi, Chennai, Kolkata, Pune, Mumbai, Ahmedabad.




Oracle fusion HCM Online Training

Oracle Fusion HCM Training

Reply
avatar
April 15, 2017 at 2:55 PM delete

High-quality records, I had come to realize approximately your blog from my buddy nandu, Hyderaba.
I've examine ate least 7 posts of yours via now, and allow me let you know, your internet site offers the
excellent and the maximum exciting facts. This is simply the type of statistics that i had
been seeking out, i am already your rss reader now and i would often watch out for the brand new posts.
for more information please visit the site
oracle fusion financials training
oracle fusion procurement online training
oracle fusion procurement training

Reply
avatar