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 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; | |
} |
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.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 { |
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
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); | |
} |
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.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) { |
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 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); | |
} |
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
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { val = x; } | |
* } | |
*/ | |
class RotateListRight { | |
static int getLength(ListNode head) { |
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.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 |
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 class ListNode { | |
// int val; | |
// ListNode next; | |
// ListNode(int x) { val = x; } | |
//} | |
public ListNode reverseList(ListNode head) { | |
ListNode curr = head; | |
ListNode prev = null, next = 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
// 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) |
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.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; |