Java 1ms solution


  • 0
    F
    public class Solution {
        public int strStr(String haystack, String needle) {
            int start = 0;
            int end = needle.length();
            
            while (end <= haystack.length()) {
                String subStr = haystack.substring(start, end);
                if (needle.equals(subStr)) return start;
                ++start;
                ++end;
            }
            
            return -1;
        }
    }

Log in to reply
 

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