This question makes no sense in Java


  • 0
    M

    This question really makes no sense in Java. We can't just make a pointer to the middle of a String... We have to make a new String anyway, so the problem is trivial using basic String methods.

    public class Solution {
        public String strStr(String haystack, String needle) {
            int pos = haystack.indexOf(needle);
            if (pos == -1)
                return null;
            return haystack.substring(pos);
        }
    }

  • 1

    I just modified the code definition to return the index instead of String. (Similar to Java's indexof method).

    Hopefully this makes more sense.

    Note: Click on the reload button to reset the code definition if you still see the old code definition.


  • 0
    J

    Tried running your code and it threw an error..


  • 0

    public class Solution {

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

    }

    Only 1 line of code can solve the problem. But this is cheating.


Log in to reply
 

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