This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |