Skip to content

Instantly share code, notes, and snippets.

@leeelton
leeelton / minEdits.swift
Created March 7, 2019 00:21
Mininum Number of Edits
func minEdit(_ s1: String, _ s2: String) -> Int {
if s1.isEmpty {
return s2.count
}
if s1.first == s2.first {
return minEdit(String(s1.suffix(s1.count-1)), String(s2.suffix(s2.count-1)))
} else if s1.count == s2.count {
return 1 + minEdit(String(s1.suffix(s1.count-1)), String(s2.suffix(s2.count-1)))
} else {
@leeelton
leeelton / DCC_56.m
Last active March 4, 2019 16:32
Daily Coding Challenge 56
/*
Given a dictionary of words and a string made up of those words (no spaces), return the original sentence in a list. If there is more than one possible reconstruction, return any of them. If there is no possible reconstruction, then return null.
For example, given the set of words 'quick', 'brown', 'the', 'fox', and the string "thequickbrownfox", you should return ['the', 'quick', 'brown', 'fox'].
Given the set of words 'bed', 'bath', 'bedbath', 'and', 'beyond', and the string "bedbathandbeyond", return either ['bed', 'bath', 'and', 'beyond] or ['bedbath', 'and', 'beyond'].
store words in a set
loop through string
build word
@leeelton
leeelton / DailyCodingChallenge55.swift
Created March 3, 2019 20:56
BFS Daily Coding Challenge 55
/*
You are given an M by N matrix consisting of booleans that represents a board. Each True boolean represents a wall. Each False boolean represents a tile you can walk on.
Given this matrix, a start coordinate, and an end coordinate, return the minimum number of steps required to reach the end coordinate from the start. If there is no possible path, then return null. You can move up, left, down, and right. You cannot move through walls. You cannot wrap around the edges of the board.
For example, given the following board:
[[f, f, f, f],
[t, t, f, t],
[f, f, f, f],
@leeelton
leeelton / DFSSwift.maze
Created March 3, 2019 20:06
DFS implementation of the classic maze
struct Pixel {
var color: Color
var isExit: Bool
init(color: Color, isExit: Bool = false) {
self.color = color
self.isExit = isExit
}
}
enum Color {
@leeelton
leeelton / QuickSelect.m
Created February 17, 2019 00:41
quick select, find nth largest number
-(void) swapFirst:(int)first withSecond:(int)second inArray:(NSMutableArray *)array
{
NSNumber *temp = array[first];
array[first] = array[second];
array[second] = temp;
return;
}
-(int) findKthLargestNumber:(int)k
inArray:(NSMutableArray *)array
@leeelton
leeelton / djikstra.m
Last active February 11, 2019 01:04
Djikstra's Algorithm in ObjC
// O(E * logV) where E is the number of Edges and V is the number of Vertices
// Why? We process every vertex with extractMin in the pqueue which results in a lgV runtime and we do this for every vertex and their neighbors (edges).
// This results in a (E + V)(lgV) runtime. In a connected graph, V = O(E) therefore our runtime complexity is just O(ElgV).
@interface Pair
@property int vertex;
@property int weight;
+(instanceType)initWithVertex:(int)vertex;
@end
@leeelton
leeelton / TrieNode.m
Last active February 11, 2019 01:12
ObjC Trie Insert and Find
// TrieNode
// TrieNodes hold a dictionary of child TrieNode objects keyed by their character.
// isValid == YES means that the word exists in the dictionary
@interface TrieNode : NSObject
@property (strong, nonatomic) NSMutableDictionary<NSString *, TrieNode *> *children;
@property (assign, nonatomic) BOOL isValid;
-(instancetype)init;
@end