For a given source string and a target string, return the index of the first occurrence target string in source string. If target does not exist in source, just return -1.

Example

Source    Target    Result
null      "abc"     -1
"abc"     null      -1
"abc"     "def"     -1
"abcd"    "bc"       1
class Solution {
    /**
     * Returns a index to the first occurrence of target in source,
     * or -1  if target is not part of source.
     * @param source string to be scanned.
     * @param target string containing the sequence of characters to match.
     */
    public int strStr(String source, String target) {
        if (source == null || target == null)
            return -1;
            
        for (int i = 0; i <= source.length() - target.length(); i++)
            if (match(source, i, target))
                return i;
        return -1;
    }
    
    private boolean match(String source, int start, String target) {
        for (int i = 0; i < target.length(); i++, start++)
            if (target.charAt(i) != source.charAt(start))
                return false;
        return true;
    }
}