Skip to content

Instantly share code, notes, and snippets.

View deedee47's full-sized avatar

Damola Kolade deedee47

View GitHub Profile
deedee47 /
Created June 5, 2021 23:03
Detects if there is at least a cyclic path in all available routes
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<>();
deedee47 /
Created May 30, 2021 01:39
Returns a list of combined words given a sequence of numbers and a Number-Letter Mapping
//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;
deedee47 /
Last active May 28, 2021 07:40
Returns minimum time difference from a given list of HH:mm time entries
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
deedee47 /
Last active May 21, 2021 06:09
Move elements in an array to the front or back depending on the set direction
//-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)
deedee47 /
Last active May 13, 2021 14:52
Merge sorted arrays into one
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){
deedee47 /
Last active May 2, 2021 15:57
Return a new array consisting of the product of all elements in the array excluding the current index
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
deedee47 /
Last active April 27, 2021 10:24
Return an array of indexes to indicate the first and last occurrence of a value in an unsorted array
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;
deedee47 /
Last active April 27, 2021 10:24
Returns new count of elements in a Numbers array excluding occurrences of a specified value
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;
deedee47 /
Last active April 12, 2021 12:16
Returns the Count of Distinct Elements from an Sorted Array of Numbers,
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++)