LeetCode – Wildcard Matching (Java)

Implement wildcard pattern matching with support for ‘?’ and ‘*’. Java Solution To understand this solution, you can use s=”aab” and p=”*ab”. public boolean isMatch(String s, String p) { int i = 0; int j = 0; int starIndex = -1; int iIndex = -1;   while (i < s.length()) { if (j < p.length() … Read more

LeetCode – Substring with Concatenation of All Words (Java)

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. For example, given: s=”barfoothefoobarman” & words=[“foo”, “bar”], return [0,9]. Analysis This problem … Read more

LeetCode – Invert Binary Tree (Java)

Java Solution 1 – Recursive public TreeNode invertTree(TreeNode root) { helper(root); return root; }   public void helper(TreeNode n){ if(n==null){ return; }   TreeNode t = n.left; n.left = n.right; n.right = t;   helper(n.left); helper(n.right); }public TreeNode invertTree(TreeNode root) { helper(root); return root; } public void helper(TreeNode n){ if(n==null){ return; } TreeNode t = … Read more

LeetCode – Implement Stack using Queues (Java)

Implement the following operations of a stack using queues. push(x) — Push element x onto stack. pop() — Removes the element on top of the stack. top() — Get the top element. empty() — Return whether the stack is empty. Note: only standard queue operations are allowed, i.e., poll(), offer(), peek(), size() and isEmpty() in … Read more

LeetCode – Populating Next Right Pointers in Each Node II (Java)

Follow up for problem “Populating Next Right Pointers in Each Node“. What if the given tree could be any binary tree? Would your previous solution still work? Analysis Similar to Populating Next Right Pointers in Each Node, we have 4 pointers at 2 levels of the tree. Java Solution public void connect(TreeLinkNode root) { if(root … Read more

LeetCode – Basic Calculator (Java)

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces.You may assume that the given expression is always valid. Some examples: “1 + 1” = 2, “(1)” = 1, “(1-(4-5))” = 2 … Read more

LeetCode – Rectangle Area (Java)

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner coordinates. Analysis This problem can be converted as a overlap internal problem. On the x-axis, there are (A,C) and (E,G); on the y-axis, there are (F,H) and (B,D). If … Read more

LeetCode – The Skyline Problem (Java)

Analysis This problem is essentially a problem of processing 2*n edges. Each edge has a x-axis value and a height value. The key part is how to use the height heap to process each edge. Java Solution class Edge { int x; int height; boolean isStart;   public Edge(int x, int height, boolean isStart) { … Read more

LeetCode – Word Ladder II (Java)

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: 1) Only one letter can be changed at a time, 2) Each intermediate word must exist in the dictionary. For example, given: start = “hit”, end = “cog”, and dict = [“hot”,”dot”,”dog”,”lot”,”log”], return: [ [“hit”,”hot”,”dot”,”dog”,”cog”], … Read more

LeetCode – Contains Duplicate III (Java)

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. Java Solution 1 – Simple This solution simple. Its time complexity is O(nlog(k)). … Read more

LeetCode – Count Complete Tree Nodes (Java)

Given a complete binary tree, count the number of nodes. The solution to this problem can be as simple as the following: public int countNodes(TreeNode root) { if(root == null){ return 0; }   return 1 + countNodes(root.left) + countNodes(root.right); }public int countNodes(TreeNode root) { if(root == null){ return 0; } return 1 + countNodes(root.left) … Read more

LeetCode – Construct Binary Tree from Preorder and Inorder Traversal (Java)

Given preorder and inorder traversal of a tree, construct the binary tree. Analysis Consider the following example: in-order: 4 2 5 (1) 6 7 3 8 pre-order: (1) 2 4 5 3 7 6 8 From the pre-order array, we know that first element is the root. We can find the root in in-order array. … Read more