Skip to content

Instantly share code, notes, and snippets.

View AlexeiDarmin's full-sized avatar
👋
software developer and simulation tinkerer

Alexei Darmin AlexeiDarmin

👋
software developer and simulation tinkerer
View GitHub Profile
@AlexeiDarmin
AlexeiDarmin / Github Cheatsheat
Last active June 10, 2016 15:04
Most frequently used git commands by me
Delete local and remote branches
git branch -D <branchName>
git push origin :<branchName>
Push local branch to remote
git push origin <branchName>
Hard Reset
git fetch --all
git reset --hard origin/<branchName>
@AlexeiDarmin
AlexeiDarmin / getPurmutations.ts
Last active November 9, 2018 21:04
Count permutations of S in B in O(B)
function getPurmutations(s: string, b: string) {
if (s.length > b.length) {
return 0
}
const desiredChars = {}
const possiblePurmutations = {} // contain the starting index for the purmutation, and the remaining characters that need to be found
let purmutationsCount = 0
for (const char of s) {
@AlexeiDarmin
AlexeiDarmin / getCommonEntries.ts
Last active November 9, 2018 21:04
getCommonEntries(arr1: number[], arr2: number[])
const array1 = [13, 27, 35, 40, 49, 55, 59]
const array2 = [17, 35, 39, 40, 55, 58, 60]
// Goal: Get common entries of two arrays, both arrays are the same length and consist of all unique elements.
// Best concievable run time = O(A) || O(B)
function getCommonEntries(arr1: number[], arr2: number[]) {
let index1 = 0
let index2 = 0
const arraySize = arr1.length
// Can make an insert, remove, replace
// find out if two strings are one or zero edits away from each others
function hasOneOrLessEdits(str1: string, str2: string): boolean {
// Best case is O(N) since the string should be the same size, if they are not then we know they the answer is false
debugger
if (Math.abs(str1.length - str2.length) > 1) {
return false
}
let countEdits = 0
@AlexeiDarmin
AlexeiDarmin / checkSubstrRotation.ts
Created November 11, 2018 17:58
Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring().
/*
Assume you have a method called isSubstring() which checks if one word is a subtring of another.
Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to
isSubstring(). Eg "waterbottle is a rotation of erbottlewat"
*/
function checkSubstr(s1: string, s2: string): boolean {
if (s1.length !== s2.length) {
return false
}
const rotationByIndex = new Map()
@AlexeiDarmin
AlexeiDarmin / rotateBitMatrix.ts
Created November 11, 2018 22:31
Rotate a NxN matrix by 90 degrees in place where each pixel in the matrix is 4 bytes.
/*
Rotate a NxN matrix by 90 degrees in place where each pixel in the matrix is 4 bytes.
*/
/* example call:
rotateMatrix(sanitizeData([
[1,2,3],
[8,9,4],
[7,6,5]
]))
@AlexeiDarmin
AlexeiDarmin / zeroOutMatrix.ts
Created November 11, 2018 23:14
If an element in an NxM matrix is zero, then every element in that row and column should be zero
// If an element in an NxM matrix is zero, then every element in that row and column should be zero
function zeroOutMatrix(m: number[][]): number[][] {
const zeroedRows = new Map()
const zeroedCols = new Map()
for (let r = 0; r < m.length; ++r) {
for (let c = 0; c < m[r].length; c++){
if (m[r][c] === 0) {
zeroedCols.set(c, true)
zeroedRows.set(r, true)
@AlexeiDarmin
AlexeiDarmin / removeDuplicates.ts
Created November 11, 2018 23:47
Remove duplicates from a linked list
function deleteDuplicates(head: Node): Node {
const occurredVals = new Map()
let node = head
occurredVals.set(node.data, true)
while (node.next != null) {
if (occurredVals.get(node.next.data) === true) {
node.next = node.next.next
} else {
occurredVals.set(node.next.data, true)
@AlexeiDarmin
AlexeiDarmin / kthToLastNode.ts
Created November 12, 2018 00:15
return the kth to last node in a linked list
function returnKthToLast(head: Node, k: number): Node | null {
let delta = 0
let node = head
while (delta < k) {
node = node.next
++delta
if (!node) {
return null
@AlexeiDarmin
AlexeiDarmin / deleteSomeNode.ts
Created November 12, 2018 00:24
Delete a middle node from a singly-linked list
function deleteSomeNode(node: Node) {
if (!node) {
return
}
if (!node.next) {
node = undefined
}
if (node.next) {