Are You Kidding?


  • -12
    C

    The Brute force algorithm with O(mn) time complexity passed oj test.

    That means I can't use the test to compare these algorithms: KMP, BM, KR, Horspool and Sunday.

    public String strStr(String haystack, String needle) {
        for(int i=0; i<=haystack.length()-needle.length(); i++)
        {
            if(haystack.substring(i, i+needle.length()).equals(needle)) return haystack.substring(i);
        }
        return null;
    }

  • -1
    L
     //I tried to use KMP, but to my surprise, even this solution is accepted! The world is simple!!
    public class Solution {
            public String strStr(String haystack, String needle) {
                int index = -1;
                index = haystack.indexOf(needle);
                if (index != -1) {
                    return haystack.substring(index);
                }
                return null;
            }
        }

  • 0
    Z

    Good luck with that on an interview... :)


  • 0
    Z

    This question wants you to write your own substring function. You cannot call the java builddin String.substring function.
    BTW, if you use java substring. You don't need to initial index to -1, the function will return -1 if not find the substring.


  • 0
    S

    get a life dude


  • 0
    R

    indexOf() and substring() can not be used, otherwise simply "return haystack.indexOf(needle);" will be accepted!


Log in to reply
 

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