Over a million developers have joined DZone.

Thursday Code Puzzler: Needles In the Haystack

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

By now you should be familiar with our Thursday Code Puzzler slot. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you think is suitable.

Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer.  It's all about the participation!

Find The Needles in the Haystack

This week it's another classic string based question - to find the number of occurances of a given String (needle) in another (haystack). So the method signature would look like:

public int findNeedleOccurances(String needle, String haystack)

Try to make the solution as efficient as possible, and if you do get to run it post up your timings, or even better, the Big O notation for your solution.

 

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}