Java in 5 lines


  • 0
    H

    public class Solution {
    public int strStr(String haystack, String needle) {
    if(haystack==null || needle==null || haystack.length()<needle.length()) return -1;
    if(haystack==needle || needle=="") return 0;
    for(int i=0; i<(haystack.length()-needle.length()+1); i++)
    if(haystack.substring(i,i+needle.length()).startsWith(needle)) return i;
    return -1;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.