Skip to content

Instantly share code, notes, and snippets.

class Solution {
public boolean checkInclusion(String s1, String s2) {
int n = s1.length();
int m = s2.length();
int[] s1Freq = new int[26];
int[] s2Freq = new int[26];
for (int i = 0; i < n; i += 1) {
s1Freq[s1.charAt(i) - 'a'] += 1;
}
@zzandland
zzandland / main.cpp
Created May 7, 2025 14:58
isBothPrefixAndSuffix
// checks if target string has key string as both prefix and suffix
bool isBothPrefixAndSuffix(string &target, string &key) {
int n = target.length(), m = key.length();
// target must be at least twice as long as key
if (n < m * 2) {
return false;
}
int lt = 0, rt = target.length() - 1, lk = 0, rk = key.length() - 1;
while (lk < m) {
if (key[lk] != target[lt] || key[rk] != target[rt]) {
void balanceSets() {
// from left to mid, elements move in to the mid
sum += balanceLR(left, mid, k);
// from mid to right, elements move out from the mid
sum -= balanceLR(mid, right, m - 2 * k);
}
int balanceLR(multiset<int> &l, multiset<int> &r, int n) {
// tracks how much should the running sum change in value
int diff = 0;
@zzandland
zzandland / percentile.py
Created December 10, 2019 05:28
Count percentile
class Node:
def __init__(self, n: int):
self.val = n
self.left = self.right = None
def percentageUnderNum(root: Node, threshold: int, depth: int) -> float:
def helper(node: Node, count: int, total: int, curDepth: int) -> [int]:
output = [count, total]
if not node or curDepth > depth:
return output
" ============================================================================
" Active plugins
" You can disable or add new ones here:
" this needs to be here, so vim-plug knows we are declaring the plugins we
" want to use
call plug#begin('~/.config/nvim/plugged')
" Now the actual plugins:
@zzandland
zzandland / count_island.py
Created August 9, 2019 00:23
count_island
class Ocean:
def __init__(self, size):
self.board = [None] * (size * size)
self.size = size
self.cnt = 0
def find(self, a):
return self.board[a] = find(self.board[a])
def union(self, a, b) -> bool:
def chunked_palindrome
#include <iostream>
#include <set>
#include <vector>
#include <math.h>
using namespace std;
int sum_sq(int n) {
int n_sqrt = sqrt(n);
set<int> sqrs;
@zzandland
zzandland / largest-level-of-tree.js
Created October 11, 2018 16:48
Largest level of tree
var findLargestLevel = (node) => {
// make levelObj as closure var
const levelObj = {};
// inner function takes in node and level
const innerFunc = (node, level) => {
// if levelObj doesn't have property at this level
if (levelObj[level] === undefined) {
// create a new value, with the node inside an array
levelObj[level] = [node];
// otherwise