This file contains 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
//http://coolcodebro.blogspot.com/2012/12/determine-if-string-has-all-unique.html | |
public class IsUnique { | |
public static void main(String[] args) { | |
new IsUnique().run(); | |
} | |
void run() { | |
String str = "123456789a0"; | |
This file contains 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
//http://coolcodebro.blogspot.com/2012/12/string-permutations.html | |
public class IsPermutation { | |
public static void main(String[] args) { | |
new IsPermutation().run(); | |
} | |
void run() { | |
String a = "12345"; | |
String b = "32541"; |
This file contains 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
//http://coolcodebro.blogspot.com/2012/12/string-compression.html | |
public class StringCompression { | |
public static void main(String[] args) { | |
new StringCompression().run(); | |
} | |
void run() { | |
String str = "aabcccccaaa"; | |
String compr = compress(str); |
This file contains 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
//http://coolcodebro.blogspot.com/2012/12/book-cracking-coding-interview-question.html | |
public class SetToZero { | |
public static void main(String[] args) { | |
new SetToZero().run(); | |
} | |
void run() { | |
int[][] matrix = { {1, 2, 3, 9, 5}, {1, 1, 0, 5, 2}}; | |
printMatrix(matrix); |
This file contains 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
//http://coolcodebro.blogspot.com/2012/12/find-out-whether-there-is-route-between.html | |
boolean isRoute(Vertex source, Vertex target) { | |
if(source == null) | |
return false; | |
if(source.equals(target)) | |
return true; | |
source.visited = true; |
This file contains 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
//http://coolcodebro.blogspot.com/2012/12/sorted-array-to-binary-search-tree.html | |
public class ArrayToBST { | |
public static void main(String[] args) { | |
new ArrayToBST().run(); | |
} | |
void run() { | |
int[] arr = {0, 4, 5, 6, 7, 9, 10, 15, 20}; | |
TreeNode root = buildBst(arr, 0, arr.length-1); |
This file contains 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
//http://coolcodebro.blogspot.com/2012/12/find-successor-of-node.html | |
TreeNode getSuccessor(TreeNode node) { | |
TreeNode n; | |
//Case 1: node has a right child | |
if(node.right != null) { | |
n = node.right; | |
while(n.left != null) | |
n = n.left; |
This file contains 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
//http://coolcodebro.blogspot.com/2012/12/merge-b-into-a.html | |
void mergeBintoA(int[] arrayA, int[] arrayB, int lastIndexA) { | |
int indexA = lastIndexA; | |
int indexB = arrayB.length-1; | |
int indexC = arrayA.length-1; | |
//Merge | |
while(indexA >= 0 && indexB >= 0) | |
arrayA[indexC--] = arrayA[indexA] > arrayB[indexB] ? arrayA[indexA--] : arrayB[indexB--]; |
This file contains 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
String[] sortByAnagrams(String[] words) { | |
Map<String, List<Integer>> map = new TreeMap<String, List<Integer>>(); | |
String[] resultArray = new String[words.length]; | |
//Map sorted words to indecies | |
for(int i=0; i<words.length; i++) { | |
char[] ch = words[i].toLowerCase().toCharArray(); | |
Arrays.sort(ch); | |
String s = String.valueOf(ch); | |
This file contains 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
//http://coolcodebro.blogspot.com/2012/12/find-string-in-array-interspersed-with.html | |
//Average case: O(log(n)), Worst case: O(n/2) => O(n) | |
void run() { | |
String[] strings = {"at", "", "", "", "ball", "byd", "", "car", "", "", "dad", "", ""}; | |
String strToFind = "byd"; | |
System.out.printf("IndexOf \"%s\": %d%n", strToFind, search(strings, strToFind)); | |
} |
OlderNewer