Skip to content

Instantly share code, notes, and snippets.

View mrboli's full-sized avatar

Bo Li mrboli

  • Toronto, ON
View GitHub Profile
/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/.test()
false
const testValue = 'sdfsdf425 - 319 - 5483'
undefined
/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/.test(testValue)
true
testValue.match(/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/)
['425 - 319 - 5483', index: 6, input: 'sdfsdf425 - 319 - 5483', groups: undefined]0: "425 - 319 - 5483"groups: undefinedindex: 6input: "sdfsdf425 - 319 - 5483"length: 1[[Prototype]]: Array(0)
testValue.match(/\(?\d{3}\)?\s*-?\s*\d{3}\s*-?\s*\d{4}/)[0].match(/\d/)
['4', index: 0, input: '425 - 319 - 5483', groups: undefined]0: "4"groups: undefinedindex: 0input: "425 - 319 - 5483"length: 1[[Prototype]]: Array(0)
@mrboli
mrboli / .tmux.conf
Created June 17, 2020 18:14
June 2020 dotfiles
# 0 IS too far from ` ;)
set -g base-index 1
# Automatically set window title
set-window-option -g automatic-rename on
set-option -g set-titles on
# set -g set-titles-string '#S - #W'
set -g set-titles-string '~'
set -g status-keys vi
@mrboli
mrboli / carry_pointer.js
Last active April 18, 2020 01:32
Number of Islands [Interesting Idea - WIP] Update all pointers
let toggleLog = false;
toggleLog = true;
let log = false;
// log = true;
const parents = {};
let numKeys = 0;
class UnionSet {
// End result: At each cell, check adjacent, if it's visited, set parent to it's parent
@mrboli
mrboli / quick_select_attempt.js
Created April 16, 2020 19:47
K Closest Points [WIP]
let distanceMemo = new Map();
var kClosest = function(points, K) {
// Choose pivot
// pseudo sort all items around pivot
// Get new partition position
// If K is larger, do again with a pivot in smaller section
// vice versa
let left = 0;
@mrboli
mrboli / classic.js
Created April 15, 2020 22:45
Word Break
var wordBreak = function(s, wordDict) {
let dict = new Set(wordDict);
let isValid = Array(s.length + 1).fill(false);
isValid[0] = true;
for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (isValid[j] && dict.has(s.substring(j, i))) {
isValid[i] = true;
break;
@mrboli
mrboli / two_array.js
Created April 15, 2020 16:24
Product of Array Except Self
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelfTwoArray = function(nums) {
const prodLeft = [];
const prodRight = [];
prodLeft[0] = 1;
prodRight[nums.length - 1] = 1;
const ans = [];
@mrboli
mrboli / no_optimization.js
Last active April 13, 2020 21:17
All Concat Words
var findAllConcatenatedWordsInADict = function(words) {
words.sort((a, b) => a.length - b.length);
let wordSet = new Set();
words.forEach(word => {
// Use Trie
if (word) wordSet.add(word);
});
// DFS
@mrboli
mrboli / simple_hash_soln.js
Last active April 12, 2020 23:59
Top K Frequent Words Heap Solution [WIP]
var topKFrequent = function(words, k, frequency = {}) {
words.forEach(word => frequency[word] = frequency[word] + 1 || 1);
return Object.keys(frequency).sort((left, right) => {
const freqDiff = frequency[right] - frequency[left];
return freqDiff === 0 ? left.localeCompare(right) : freqDiff;
}).slice(0, k);
};
@mrboli
mrboli / autocomplete.js
Created April 12, 2020 18:38
Autocomplete
/**
* @param {string[]} sentences
* @param {number[]} times
*/
var AutocompleteSystem = function(sentences, times) {
this.trie = { letters: {} };
this.sentences = {};
this.searchInput = '';
sentences.forEach((sentence, sentenceIdx) => {
@mrboli
mrboli / solution.js
Created April 2, 2020 04:06
Single Number
// hashmap()
// 56 ms, faster than 87.12%
// 37.2 MB, less than 50.00%
function hashmap (nums) {
let existance = {};
for (let i = 0; i < nums.length; i++) {
if (nums[i] in existance === false)
existance[nums[i]] = 1;
else
delete existance[nums[i]];