View checkCyclicPath.java
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
public boolean isThereCycle(Map<Character, List<Character>> map){ | |
if(map == null || map.isEmpty()) return false; | |
//use each node as starting point | |
for(char item : map.keySet()){ | |
//create new tracking experience for each start point | |
Stack<Character> trackPad = new Stack<>(); | |
List<Character> visitedPoints = new ArrayList<>(); | |
visitedPoints.add(item); |
View playNumberLetterCombination.java
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
//Keypad Number to Letter Mapping | |
//2-9 corresponds to letters on the phone keypad and the same index of the array | |
private static String[] keyPadMapping = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv" ,"wxyz"}; | |
public static List<String> playNumberLetterCombination(String number){ | |
List<String> combinedWords = new ArrayList<>(); | |
//empty or wrong input | |
if(number.isEmpty() || !number.matches("\\d+")) return combinedWords; |
View getMinTimeDifference.java
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
public long getMinDifferenceInMinutes(String[] journal){ | |
if(journal == null) return 0; | |
if(journal.length == 0) return 0; | |
//Using a set to store unique and valid entries | |
//if any entry is duplicated, it will auto break because that will be the minimum time difference | |
//TreeSet stores data in natural order - O(Log N) time complexity - better than sorting array with NLogN | |
//sorting keeps times close together and will eliminate comparing each entry with all other elements | |
//Tradeoff extra space for faster sort process |
View shuffleLine.java
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
//-1 move student from front of line to back | |
//+1 move student from back of line to front | |
//0 no movement | |
public int[] shuffleLine(int[] line, int numberToShuffle){ | |
if(line == null) return new int[0]; | |
int lineLength = line.length; | |
if(numberToShuffle == 0 || lineLength == 0 || Math.abs(numberToShuffle) == lineLength) return line; | |
if(Math.abs(numberToShuffle) > lineLength) |
View mergeSortedArrays.java
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
public int[] mergeSortedArray(int[] classA, int[] classB){ | |
if(classA == null && classB == null) return new int[0]; | |
else if (classA == null) return classB; | |
else if (classB == null) return classA; | |
int aIndex = 0, bIndex = 0, mergeIndex = 0, classLength = classA.length+classB.length; | |
int[] merged = new int[classLength]; | |
while(mergeIndex < classLength){ | |
if(aIndex<classA.length && bIndex<classB.length){ |
View getProductArray.java
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
public int[] getProducts(int[] nums){ | |
if(nums == null) return new int[0]; | |
if(nums.length == 0) return new int[0]; | |
if(nums.length == 1) return new int[]{0}; | |
int[] products = new int[nums.length]; | |
//any number multiplied by 1 is the same number; therefore allProducts cannot be initialized to 0 |
View boundaryIndexesOfVal.java
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
public int[] getBoundaryIndexesOfVal(int[] nums, int val){ | |
if(nums == null) return new int[] {-1, -1}; | |
if (nums.length == 0) return new int[] {-1, -1}; | |
int startIndex = 0; | |
int endIndex = nums.length - 1; | |
//keep count of elements before and after the specified value | |
int preValCount = 0; |
View countOfElementsExcludingVal.java
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
public int countOfElementsExcludingVal(int[] nums, int val){ | |
if(nums == null) return 0; | |
int countWithoutVal = nums.length; | |
for(int item : nums){ | |
if (item == val) countWithoutVal--; | |
} | |
return countWithoutVal; |
View UniqueCountOfElements.java
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
public int uniqueCountOfElements(int[] nums) | |
{ | |
if (nums == null) return 0; | |
int arrSize = nums.length; | |
if (arrSize == 0) return 0; | |
int finalCount = 1; // Count the first element by default | |
for(int count = 0; count < arrSize; count++) | |
{ |