1ms Java solution


  • -1
    V

    public class Solution {

    public int strStr(String haystack, String needle) {
        
        if(needle.length() == 0) {
            return 0;
        }
        
        if(needle.length() > haystack.length()) {
            return -1;
        }
        
        for(int i = 0, j = needle.length(); j <= haystack.length(); i++, j++) {
            if(haystack.substring(i,j).equals(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.