Why not use indexOf()?


  • 2
    E

    There's no limit to use indexOf, so Why not use indexOf()?

    public int strStr(String haystack, String needle) {
            return haystack.indexOf(needle, 0);
        }
    

  • 0

    @ermao Of course you can use built-in functions, but would you expect the interviewer want you to use it and ask nothing more? Ask yourself is this what you really want from the Leetcode, cheating yourself? Come on, man!


  • 1
    J

    However, I don't think everybody knows to use built-in func at first time, this solution is definitely a good way to figure out this problem.


  • 0

    @jumpdewey Then you must think everyone else is just fool...of course this is the most straightforward solution in the very first place...


  • 1
    J

    @LHearen of course your reputation said you are a good coder, and I'm a freshman. :) BUT NOT A FOOL


  • 0

    @jumpdewey You're getting little bit carried away on this, man. Sorry to be so aggressive before, under lots of pressure these days.

    I just wanted to say that we are doing algorithm so we should pay more attention to the details, the underlying implementations instead of the built-in functions which can be a very good reference, but now we should be the implementator instead of the library builder.

    Enjoy yourself! Sorry about my rudeness.


  • 1

    I'd like to point this out more clearly:

    Why not use indexOf()?

    Because "LeetCode OJ is a platform for preparing technical coding interviews" and you're not exactly going to impress your interviewers with that. Instead they'll be more like "WTF, are you stupid? Obviously I want to see whether and how you can implement indexOf".


  • 3
    C

    well, just use it


  • 1

    As far as I know String indexOf() method does the same thing as described in the problem, and I believe the main point of the problem would be to implement in a lower level without any built-in method.

    But why was my code still accepted? :(

    beating 96.90% of javasubmissions, 0ms

    public class Solution { public int strStr(String haystack, String needle) { return haystack.indexOf(needle); }}
    

    https://leetcode.com/submissions/detail/69394135/

    In the interview could I write two solutions? The first one with indexOf() while implementing the strStr without indexOf() in the second one, such that would this impress the interviewer of my familiarity of this method, what's more, of the ability to implement it as well?

    Anyway, I am going to solve it without indexOf() later :)


  • 2

    @BryanBo.Cao Anyway, accept is justice :)


  • 0

    @Zoey_Yue Leetcode High!


  • 0
    C

    @StefanPochmann So in this kind of question, we should not use indexOf(). When I see some solutions to these questions, some used subString(), is it also a build-in method? So I don't what kind of build-in method we can use to do this kind of questions.


  • 0
    N

    I saw many other problems in Leetcode are solved by so called built-in functions.

    For example, the question of pairing parentheses, how would you do this without using the "built-in functions" of push/pop for stack? Or you just write a push function yourself? I did this by calculating the sum of the index difference between the right parentheses and the left parentheses for each type of pair. But I think using stacks is brilliant.

    I do not think any interview would have this kind of things nowadays.


  • -2
    C

    I think indexOf method will cost more time than did by yourself. The score of indexOf is 60%, but my solution is 87.6%. indexOf method has much useless logic like

    if (fromIndex >= sourceCount) {
        return (targetCount == 0 ? sourceCount : -1);
     }
     if (fromIndex < 0) {
                fromIndex = 0;
    }
    

    and the int max = sourceOffset + (sourceCount - targetCount); we just can change to int max = haystack.length() - needles.length;. The following in my solution from indexOf without useless code.

    if (needle.length() == 0) {
                return 0;
            }
            if (haystack.length() == 0) {
                return -1;
            }
            char haystacks[] = haystack.toCharArray();
            char needles[] = needle.toCharArray();
            char first = needles[0];
            int max = haystack.length() - needles.length;
            for (int i = 0; i <= max; i++) {
                if (haystacks[i] != first) {
                    while (++i <= max && haystacks[i] != first) ;
                }
    
                if (i <= max) {
                    int j = i + 1;
                    int end = j + needles.length - 1;
    
                    for (int k = 1; j < end && haystacks[j] == needles[k]; k++, j++) {
    
                    }
                    if (j == end) return i;
                }
            }
            return -1;
    

  • 0
    M

    @wangchunlei said in Why not use indexOf()?:

    The score of indexOf is 60%

    I just submitted it three times, beat 72.42%, 99.54% and 99.54%.

    but my solution is 87.6%

    I just submitted it three times, beat 72.42%, 72.42% and 65.78%


  • 0
    C

    @ManuelP Does LeetCode occur different score if we submit twice?


Log in to reply
 

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