Count Occurence of Needle String in Haystack String, most optimally?

find a needle in haystack all permutations
needle in a haystack problem
find out how many times a particular digit occurs in a number in python

The Problem is simple Find "ABC" in "ABCDSGDABCSAGAABCCCCAAABAABC" without using String.split("ABC")

Here is the solution I propose, I'm looking for any solutions that might be better than this one.

public static void main(String[] args) {
 String haystack = "ABCDSGDABCSAGAABCCCCAAABAABC";
 String needle = "ABC";
 char [] needl = needle.toCharArray();
 int needleLen = needle.length();
 int found=0;
 char hay[] = haystack.toCharArray();
 int index =0;
 int chMatched =0;

 for (int i=0; i<hay.length; i++){

  if (index >= needleLen || chMatched==0)
   index=0;
  System.out.print("\nchar-->"+hay[i] + ", with->"+needl[index]);

  if(hay[i] == needl[index]){
   chMatched++;
   System.out.println(", matched");
  }else {
   chMatched=0;
   index=0;
   if(hay[i] == needl[index]){
    chMatched++;
    System.out.print("\nchar->"+hay[i] + ", with->"+needl[index]);
    System.out.print(", matched");
   }else
   continue;
  }

  if(chMatched == needleLen){
   found++;
   System.out.println("found. Total ->"+found);
  }
  index++;
 } 
 System.out.println("Result Found-->"+found);
 }

It took me a while creating this one. Can someone suggest a better solution (if any) P.S. Drop the sysouts if they look messy to you.

How about:

boolean found = haystack.indexOf("ABC") >= 0;

**Edit - The question asks for number of occurences, so here's a modified version of the above:

public static void main(String[] args)
{
    String needle = "ABC";
    String haystack = "ABCDSGDABCSAGAABCCCCAAABAABC";

    int numberOfOccurences = 0;
    int index = haystack.indexOf(needle);
    while (index != -1)
    {
        numberOfOccurences++;
        haystack = haystack.substring(index+needle.length());
        index = haystack.indexOf(needle);
    }

    System.out.println("" + numberOfOccurences);
}

Count Occurrences of a Char in a String, Learn how to count characters with the core Java library and with libraries and There are many ways to count the number of occurrences of a char in a String in Java. Learn how to solve the "needle in a haystack" problem by using the is technically correct, but sub-optimal, as it's overkill to use the very� Count Occurence of Needle String in Haystack String, most optimally? Ask Question a simple indexOf() test will suffice. If you need to know the number of occurrences, as your posted code tries to find, a simple approach would be to use a regex:

If you're looking for an algorithm, google for "Boyer-Moore". You can do this in sub-linear time.

edit to clarify and hopefully make all the purists happy: the time bound on Boyer-Moore is, formally speaking, linear. However the effective performance is often such that you do many fewer comparisons than you would with a simpler approach, and in particular you can often skip through the "haystack" string without having to check each character.

Anagram Substring Search (Or Search for all permutations , 1) The first count array store frequencies of characters in pattern. …..a) If the two count arrays are identical, we found an occurrence. incorrect, or you want to share more information about the topic discussed above Count of binary strings of length N having equal count of 0's and 1's and count of 1's� substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive. Note: This function doesn't count overlapped substrings. See the example below!

You say your challenge is to find ABC within a string. If all you need is to know if ABC exists within the string, a simple indexOf() test will suffice.

If you need to know the number of occurrences, as your posted code tries to find, a simple approach would be to use a regex:

public static int countOccurrences(string haystack, string regexToFind) {
   Pattern p = Pattern.compile(regexToFind);
   Matcher m = p.matcher(haystack); // get a matcher object
   int count = 0;
   while(m.find()) {
       count++;
   }
   return count;
}

substr_count - Manual, substr_count — Count the number of substring occurrences. Description �. substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) : int. a.b.c.d I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner. 2246/counting-occurrence-particular-character-inside-string-java Toggle navigation

Have a look at http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm

Java String count letter occurrences, count occurrences of character in string java using hashmap More info in public static int countOccurrences(String haystack, char needle) { int count Not optimal, but simple way to count occurrences: String s = ""; int counter = s.split("\\$ ",� Parameters. haystack. The string to search in. needle. If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. This behavior is deprecated as of PHP 7.3.0, and relying on it is highly discouraged.

public class NeedleCount
{
  public static void main(String[] args)
  {
    String s="AVBVDABCHJHDFABCJKHKHF",ned="ABC";
    int nedIndex=-1,count=0,totalNed=0;
    for(int i=0;i<s.length();i++)
    {
      if(i>ned.length()-1)
        nedIndex++;
      else
        nedIndex=i;
      if(s.charAt(i)==ned.charAt(nedIndex))
        count++;
      else
      {
        nedIndex=0;
        count=0;
         if(s.charAt(i)==ned.charAt(nedIndex))
          count++;
        else
          nedIndex=-1;
      }
      if(count==ned.length())
      {
        nedIndex=-1;
        count=0;
        totalNed++;
        System.out.println(totalNed+" needle found at index="+(i-(ned.length()-1)));
      }
    }
    System.out.print("Total Ned="+totalNed);
  }
}

Boyer–Moore string-search algorithm, In computer science, the Boyer–Moore string-search algorithm is an efficient string-searching S[i] denotes the character at index i of string S, counting from 1 . The Boyer–Moore algorithm searches for occurrences of P in T by performing explicit Boyer–Moore uses information gained by preprocessing P to skip as many� 1 Count Occurence of Needle String in Haystack String, most optimally? Dec 26 '17. 0 How to recursively remove all adjacent duplicates Jan 9 '18.

String-searching algorithm, In computer science, string-searching algorithms, sometimes called string- matching algorithms, The goal is to find one or more occurrences of the needle within the haystack. purposes, or polymorphisms that lead to no change in the encoded proteins, which may not count as a true difference for some other purposes. Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa", needle = "bba" Output:-1 Clarification: What should we return when needle is an empty string? This is a great question to ask during an interview. For the purpose of this problem, we will return 0 when needle is an empty string.

Count occurrences of a substring, PROC count string in string = (STRING needle, haystack)INT: ( in this case) would offer most languages a simple and immediate solution. A Needle in the Haystack Our hacker, Little Stuart lately has been fascinated by ancient puzzles. One day going through some really old books he finds something scribbled on the corner of a page.

String functions geeksforgeeks, For more info, please refer this. strdup ( ) Duplicates the string. and stars, optimal polygon triangulation dynamic programming, count the number of of the first occurrence of a string inside another string (case-sensitive) strrchr() Finds the specified in needle, or a null pointer if the sequence is not present in haystack. public static int CountOccurences(string haystack, string needle) { return (haystack.Length - haystack.Replace(needle, string.Empty).Length) / needle.Length; } share | improve this answer answered Feb 27 at 10:45

Comments
  • Be so kind as to indent every line of code with four spaces, that will make it more readable and add syntax highlighting. P.S.: If it's some kind of homework, add it as a tag ;)
  • "Better" and "optimally" in terms of what? Memory footprint? Speed? Maintainability?
  • Performance, and performance. Memory is not an issue.
  • Should be done iteratively, each time starting at 1 beyond the last found pos (or "ABC".length() beyond index) to count the amount of occurrences (which is the question).
  • nah, that only says yes or no. I asked for count. and yes don't use String.Split("ABC")
  • OK, that wasn't particularly clear from your question which simply asked to find the string "ABC" in a longer string.
  • Boyer-Moore is O(N), i.e. linear.
  • Well when you do it right, you can skip through the text (the "haystack", in terms of this question) such that you don't have to look at every character. It depends on the search string, of course; if the search string is one character long, then it's always just linear. And yes, formally the worst-case performance is linear (I think it's 3n right?)
  • Nice article on wikipedia about this!
  • @Pointy Of course, but that does not change the fact that it is linear. Complexity analysis does not care about constants, so it's O(N). There is no general guarantee that Boyer-Moore will be faster than a naive approach, it all depends on the string being searched.