Personal Website and Computer Science TUR EN

Interview Question Implement strStr()

Last update: 1/31/2017 10:25:00 AM


The problem is to implement a substring method that takes two parameters. If the first one includes the second one, then it returns the index of the first occurrence of that. Otherwise, -1 will be returned.

Problem #28 - Implement strStr().

Description: Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.ImplementStrStr;
 * @author fkabakci
 * Problem Description: Implement strStr().
 * Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack
 * Solution: (Not the best !)
 * 1. Do brute force. Compare character by character.
 * 2. If not equals,
 * 	     then immediately pass to next character. Then look again from it to length of needle.
 * 3. Check if current length of haystack is less than length of needle in each cycle,
 * 	  If so,
 * 		 then return -1.
public class ImplementStrStr {
	public int strStr(String haystack, String needle) {
		int hLength = haystack.length(), nLength = needle.length();
		if(nLength == 0)
			return 0;
		if(hLength == 0)
			return -1;

		for (int i = 0; nLength <= hLength - i; i++) {
			for (int j = 0; haystack.charAt(i + j) == needle.charAt(j); j++)
				if(j == nLength - 1)
					return i;
		return -1;
	public static void main(String[] args) {
		int index = new ImplementStrStr().strStr("", "");

There has been no comment yet


   Please verify the image

The Topics in Computer Science

Search this site for


Software & Algorithms


In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages


A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#



A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.



Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies


Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies


Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME



A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems


An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science


Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.