This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Given two strings s and p, return an array of all the start indices of p's anagrams in s. | |
You may return the answer in any order. | |
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, | |
typically using all the original letters exactly once. | |
""" | |
from collections import Counter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Given a binary array nums and an integer k, | |
return the maximum number of consecutive 1's in the array if you can flip at most k 0's. | |
""" | |
class Solution: | |
def longestOnes(self, nums: List[int], k: int) -> int: | |
left = 0 | |
zero_count = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This isn't the most optimal kotlin code. but it is one that solves all cases. | |
class Solution { | |
fun characterReplacement(s: String, k: Int): Int { | |
val n = s.length | |
val maxCharMap = mutableMapOf<Char, Int>(); | |
var left = 0 | |
var maxCount = Float.NEGATIVE_INFINITY.toInt() | |
var newMax = maxCount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import functools | |
import time | |
def fact(n): | |
if n < 2: return 1 | |
# this will still produce stack returns to return back. | |
return n * fact(n-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Dict | |
def find_optimal_stations(): | |
places_to_cover = set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]) | |
hashset_places: Dict[set] = { | |
"kone": set(("id", "nv", "uv")), | |
"ktwo": set(["wa", "id", "mt"]), | |
"k3": set(["or", "nv", "ca"]), | |
"k4": set(["nv", "ut"]), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Optional, List | |
from collections import defaultdict | |
from itertools import groupby | |
def grouped_anagram(anagram_arr: List[str]) -> List[List[str]]: | |
w_group = defaultdict(list) | |
for key_sum, word in groupby(anagram_arr, lambda w: sum(map(ord, w))): | |
w_group[key_sum] += list(word) | |
return list(w_group.values()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def happy_number(x: int, result=0, seen: Optional[set] = None) -> bool: | |
seen = seen or set() | |
num_to_sqr, new_num = ((x%10)**2 + result), x//10 | |
# print(f"1st num_to_sqr={num_to_sqr} new_num={new_num}") | |
seen.add(num_to_sqr) | |
# print(f"seen={seen}") | |
if new_num: | |
res = happy_number(new_num, num_to_sqr, seen) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools as fnctl | |
print(fnctl.reduce(lambda x, y: x^y, (4,1,2,1,2))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def firstMissingPositive(self, nums: List[int]) -> int: | |
# filter non negative, new size n | |
arr = sorted(filter(lambda x: x > 0, nums)) | |
count = len(arr) | |
# filter all num <= n to a hash set and keep new count, count. | |
arr = filter(lambda x: x <= count, set(arr)) | |
greater_positive_num = 1 | |
for num in arr: | |
if greater_positive_num == num: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# print(is_string_rotate("ABRACADA", "RACADAAB")) | |
# 1. increment string 1 char count | |
# 2. check string 2 count, inc & skip until match. if word count doesn't | |
# match in step 1, move to next character. | |
# 3. Return to step 1 | |
# 4. Invariant: End when string1 count is exhausted. | |
def is_string_rotate(string1: str, string2: str) -> bool: | |
"""Returns True if string is rotated. | |