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 October 4, 2017 00:31
Return true iff the two given strings have the same character frequencies.
static boolean isAnagram(String a, String b) {
if(a.length() != b.length()) return false;
return Arrays.equals(getCount(a.toLowerCase()), getCount(b.toLowerCase()));
}
static int[] getCount(String str) {
int[] count = new int[26];
str.chars().forEach(ch -> count[ch-'a']++);
return count;
}
@shailrshah
shailrshah / MapSort.java
Last active October 4, 2017 18:07
Functions to sort a Map by either keys or values, either ascending or descending
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.LinkedHashMap;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.Comparator;
class MapSort {
@shailrshah
shailrshah / FunctionalInterFaceExample.java
Created October 7, 2017 00:00
An example to show how lambda expressions are mapped to the abstract mentods in functional interfaces
class FunctionalInterFaceExample {
/* Each lambda corresponds to a given type, specified by an interface.
A so called functional interface must contain exactly one abstract method declaration.
Each lambda expression of that type will be matched to this abstract method. */
@FunctionalInterface
interface Converter<F, T> {
T convert(F from);
}
@shailrshah
shailrshah / PermuteString.java
Last active October 15, 2017 16:16
A function to compute all the possible permutations of a given string.
import java.util.List;
import java.util.ArrayList;
import java.time.Instant;
import java.time.Duration;
class PermuteString {
static List<String> permutations = new ArrayList<>();
private static void permuteHelper(String availableChars, String permutationSoFar) {
@shailrshah
shailrshah / GCDLCM.java
Last active October 10, 2017 17:20
Calculate the GCD and LCM of two numbers
static int getGCD(int a, int b) {
return (a == 0 || b == 0) ? (a + b) : (getGCD(b, a % b));
}
static int getLCM(int a, int b) {
return (a * b) / getGCD(a, b);
}
@shailrshah
shailrshah / RotateListRight.java
Created October 11, 2017 17:05
Rotate List: Given a list, rotate the list to the right by k places, where k is non-negative. (https://leetcode.com/problems/rotate-list/description/)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class RotateListRight {
static int getLength(ListNode head) {
@shailrshah
shailrshah / ClockAngle.java
Created October 12, 2017 02:54
Given a time in HH:MM:SS, find the angles made by the hour and minute hands, and the minutes and seconds hands.
import java.util.Scanner;
class ClockAngle {
// For hour
// 360* -> 12 hours or 12*60 minutes
// 0.5* -> 1 minute
// Angle wrt 00:00 = 0.5 * (60H + M)
// For Minute
// 360* -> 60 minutes
@shailrshah
shailrshah / LinkedListReverse.java
Created October 13, 2017 01:26
Given a linked list, reverse it.
//public class ListNode {
// int val;
// ListNode next;
// ListNode(int x) { val = x; }
//}
public ListNode reverseList(ListNode head) {
ListNode curr = head;
ListNode prev = null, next = null;
@shailrshah
shailrshah / RotateArray.java
Last active November 17, 2017 16:17
Rotate an array of n elements to the right or left by k steps.
// rotate([1, 2, 3, 4, 5], 2, 'r')
// [5, 4, | 3, 2, 1] reverse(nums, 0, n-1)
// [4, 5, | 3, 2, 1] reverse(nums, 0, k-1)
// [4, 5, | 1, 2, 3] reverse(nums, k, n-1)
// rotate([1, 2, 3, 4, 5], 2, 'l')
// [5, 4, 3, | 2, 1] reverse(nums, 0, n-1)
// [3, 4, 5, | 2, 1] reverse(nums, 0, n-k-1)
// [3, 4, 5, | 1, 2] reverse(nums, n-k, n-1)
@shailrshah
shailrshah / Fib.java
Created October 15, 2017 16:52
Return the nth number in the Fibonacci Sequence
import java.util.Map;
import java.util.HashMap;
class Fib {
static long getFibIterative(int n) {
if(n < 2)
return n;
long a = 0, b = 1, c = a + b;
for(int i = 2; i <= n; c = a+b, a = b, b = c, i++);
return c;