Simple Java Solution


  • 0
    J

    Simple solution utilizing recursion

    public class Solution {
        public int strStr(String haystack, String needle) {
            return strStr(haystack, needle, 0);
        }
        
        public int strStr(String haystack, String needle, int place) {
            if(haystack.length() < needle.length()) return -1;
            if(haystack.substring(0, needle.length()).equals(needle)) return place;
            return strStr(haystack.substring(1), needle, place+1);
        }
    }
    

  • 0
    H

    To me, it's basically a brute force algo, but why on earth is it so fast?


Log in to reply
 

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