Implement strStr() in Java - LeetCode

Problem : Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.




Java Solution 1:


public class Solution {
    public int strStr(String haystack, String needle) {
        if(haystack==null || needle==null)
           return -1;
        if(haystack.length()==0 && needle.length()==0)
           return 0;
        return haystack.indexOf(needle);    
    }
}


Time Complexity : O(n)   Space Complexity : O(n)


Java Solution 2:

Binary Tree Zigzag Level Order Traversal in Java - LeetCode

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:

Given Binary Tree [3,9,20,null,null,15,7]

    3
   /  \
 9    20
     /      \
   15      7


return its zigzag level order traversal as :

[
   [3],
   [20,9],
   [15,7]
]




Java Solution :