Skip to content

Instantly share code, notes, and snippets.

View shaaslam's full-sized avatar

Shahrukh Aslam shaaslam

View GitHub Profile
@shaaslam
shaaslam / Java
Created January 7, 2017 01:52
Rotate String to the right by k steps.
public static String rotate(String string, int k){
if(string == null) return string;
if(string.length() < 2) return string;
if(k > string.length()) k = k % string.length();
char[] result = new char[string.length()];
for(int i = 0; i < k; i++){
result[i] = string.charAt(string.length() - k + i);
}
@shaaslam
shaaslam / TinyUtils
Created August 31, 2017 21:18
Generate Hash
package com.shaaslam.tiny;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class TinyUtils {
private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final long BASE = ALPHABET.length();
@shaaslam
shaaslam / Subset.java
Last active February 12, 2018 22:43
Print out all the subsets of a set in order.
import java.util.Arrays;
public class Subsets {
public static void main(String[] args) {
String array[] = {"a", "b", "c"};
System.out.println(Arrays.deepToString(subset(array)));
}
public static String[] subset(String array[]) {
@shaaslam
shaaslam / DoublePower.java
Created October 23, 2017 00:24
Power function to raise a double to an int power, including negative powers.
public class DoublePower {
public static void main(String[] args) {
double d = 5;
int n = 2;
System.out.println(pow(d,n));
}
public static double pow(double d, int e) {
if(e == 0) return 1; // base case --> if exponent is zero, answer is 1
@shaaslam
shaaslam / IsBST.java
Last active November 5, 2017 04:19
Is valid BST?
public class isBST {
public static void main(String[] args) {
int array[] = {30, 85, 5};
Arrays.sort(array);
TreeNode node = TreeNode.createMinimalBST(array);
node.print();
System.out.println(node.isBST());
System.out.println(isValidBST(node));
package tree;
import java.util.ArrayList;
import java.util.Arrays;
public class PrintAllPaths {
public static void main(String[] args) {
int array[] = {30, 20, 54, 85, 10, 46, 5, 13, 26};
Arrays.sort(array);
/*
* Let T be a rooted tree. The lowest common ancestor between two nodes n1 and n2 is defined
as the lowest node in T that has both n1 and n2 as descendants.
The LCA of n1 and n2 in T is the shared ancestor of n1 and n2 that is located farthest from the
root. Computation of lowest common ancestors may be useful, for instance, as part of a
procedure for determining the distance between pairs of nodes in a tree: the distance from n1
to n2 can be computed as the distance from the root to n1, plus the distance from the root to
n2, minus twice the distance from the root to their lowest common ancestor. (Source: Wikipedia)
Design an write an algorithm to find the LCA node, given two nodes in a Binary Tree.
public class Flip {
public static void main(String[] args) {
int array[] = {1,2,3,4,5,6,7};
TreeNode node = TreeNode.createMinimalBST(array);
node.print();
flip(node);
node.print();
}
import java.util.HashMap;
public class PhoneDialer {
public static HashMap<String, String> map = new HashMap<>();
static {
map.put("0", "0");
map.put("1", "1");
map.put("2", "ABC");
map.put("3", "DEF");
import java.util.Arrays;
public class Successor {
public static void main(String[] args) {
int array[] = {30, 20, 54, 85, 10, 46, 5, 13, 26};
Arrays.sort(array);
TreeNode node = TreeNode.createMinimalBST(array);
node.print();