Skip to content

Instantly share code, notes, and snippets.

View shailrshah's full-sized avatar
🖥️
Available

Shail Shah shailrshah

🖥️
Available
View GitHub Profile
@shailrshah
shailrshah / Anagrams.java
Last active November 6, 2017 03:39
Given two strings, are they both anagrams of each other?
import java.util.Arrays;
class Anagrams {
static int primes = getPrimeNumbers(26); // {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
public boolean isAnagram(String s, String t) {
char[] counts = new char[26];
for(int i = 0; i < s.length(); i++) counts[s.charAt(i)-'a']++;
for(int j = 0; j < t.length(); j++) counts[t.charAt(j)-'a']--;
for(int i = 0; i < counts.length; i++)
@shailrshah
shailrshah / getPost.js
Last active October 18, 2017 21:49
GET an POST json objects
const root = "https://jsonplaceholder.typicode.com"
const getURL = "/posts/1";
const postURL = "/posts";
const getInput = () => {
return fetch(root + getURL)
.then((response) => {
console.log(response.status + " " + response.ok);
return response.json();
});
@shailrshah
shailrshah / productExceptSelf.java
Last active October 31, 2017 13:32
You have an array of integers, and for each index you want to find the product of every integer except the integer at that index.
// 1 2 3 4 nums
// 1 2 6 24 forward
// 24 24 12 4 backward
// 24 12 8 6 final nums
public int[] productExceptSelf(int[] nums) {
return getProductExceptSelf(nums, buildForward(nums), buildBackward(nums));
}
private static int[] buildForward(int[] nums) {
int[] forward = new int[nums.length];
@shailrshah
shailrshah / MergeSortedArrays.java
Last active November 2, 2017 13:28
Given two sorted integer arrays a[] and b[], merge b[] into a[] as one sorted array.
public void merge(int[] a, int m, int[] b, int n) {
int i = m - 1;
int j = n - 1;
int k = m + n - 1;
while(i >= 0 && j >= 0)
a[k--] = a[i] > b[j] ? a[i--] : b[j--];
while(i >= 0)
a[k--] = a[i--];
private boolean isValidBSTHelper(TreeNode root, int min, int max) {
if(root == null)
return true;
if(root.val < min || root.val > max)
return false;
if(root.val == Integer.MIN_VALUE && root.left != null)
return false;
if(root.val == Integer.MAX_VALUE && root.right != null)
@shailrshah
shailrshah / KthSmallest.java
Created October 21, 2017 00:26
Find the Kth smallest element in a BST.
static int count, element;
private static void kthSmallestHelper(TreeNode root, int k) {
if(root == null)
return;
kthSmallestHelper(root.left, k);
if(k == ++count) {
element = root.val;
@shailrshah
shailrshah / ZigzagLevelOrder.java
Created October 21, 2017 19:30
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).
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> answer = new ArrayList<>();
zigzagLevelOrderHelper(root, answer, 0);
return answer;
}
private void zigzagLevelOrderHelper(TreeNode root, List<List<Integer>> answer, int level) {
if(root == null)
@shailrshah
shailrshah / DetectLLCycle.java
Created October 21, 2017 19:40
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
//Floyd's cycle-finding algorithm
public ListNode detectCycle(ListNode head) {
if(head == null)
return null;
ListNode fast = head;
ListNode slow = head;
while(fast.next != null && fast.next.next != null) {
fast = fast.next.next;
@shailrshah
shailrshah / BalancedTree.java
Created October 22, 2017 03:34
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
public boolean isBalanced(TreeNode root) {
return getHeight(root) >= 0;
}
private int getHeight(TreeNode root) {
if(root == null)
return 0;
int leftHeight = getHeight(root.left);
if(leftHeight < 0)
@shailrshah
shailrshah / MinTreeDepth.java
Created October 22, 2017 18:21
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
public int minDepth(TreeNode root) {
if(root == null)
return 0;
if(root.left == null && root.right != null)
return 1 + minDepth(root.right);
if(root.right == null && root.left != null)
return 1 + minDepth(root.left);