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 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); | |
} |
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
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(); | |
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; | |
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[]) { |
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 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 |
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 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)); |
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
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); |
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
/* | |
* 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. |
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 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(); | |
} |
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.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"); |
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; | |
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(); |
OlderNewer