Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
tag : substring
public class Solution { public int strStr(String haystack, String needle) { if(haystack == null || needle == null || haystack.length() < needle.length()) { return -1; } int index = -1; for(int i = 0; i <= haystack.length() - needle.length(); i++) { int j; for(j = 0; j < needle.length(); j++) { if(needle.charAt(j) != haystack.charAt(i + j)) { break; } } if(j == needle.length()) { return i; } } return index; } }