Skip to content

Instantly share code, notes, and snippets.

View ssshukla26's full-sized avatar
💭
Learn Apply Repeat

Sunny Shukla ssshukla26

💭
Learn Apply Repeat
View GitHub Profile
@ssshukla26
ssshukla26 / BTEncDec.py
Created November 27, 2021 03:25
Serialize and Deserialize Binary Tree [LeetCode 297]
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Codec:
def serialize(self, root):
@ssshukla26
ssshukla26 / PrimeFactors.py
Last active November 24, 2021 06:47
Get all prime factors of a number
# Function to find prime factors of a number
def primeFactors(num):
# Function to reduce num by a factor
def reduceByFactor(factor):
nonlocal num
while num % factor == 0:
num = num // factor
return
@ssshukla26
ssshukla26 / DeleteNodeBST.py
Created November 22, 2021 18:03
Delete Node from a BST [LeetCode 450]
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deleteAndReplaceWithMin(self, root: TreeNode):
@ssshukla26
ssshukla26 / FromPostOrder.py
Created November 21, 2021 01:05
Build Tree From Preorder and Postorder given Inorder [LeetCode 105/106]
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
@ssshukla26
ssshukla26 / ValidIP.py
Created November 14, 2021 23:54
Validate IP Address [LeetCode 468]
import re
class Solution:
def validIPAddress(self, queryIP: str) -> str:
def is_ipv4(ip: str) -> bool:
ip = ip.split(".")
if len(ip) == 4: # must have 4 different parts
@ssshukla26
ssshukla26 / MaxStack.py
Created November 9, 2021 18:54
Max Stack [LeetCode 716]
from sortedcontainers import SortedDict
class Element:
def __init__(self, v):
self.v = v # Current value
self.p = None # Previous pointer
self.n = None # Next Pointer
return
class MaxStack:
@ssshukla26
ssshukla26 / LFUCache.py
Created October 14, 2021 00:29
LFU Cache [LeetCode 460]
# Reference : https://www.youtube.com/watch?v=Jn4mbZVkeik
from collections import OrderedDict
# A class to hold the value and the counter of the key
class KeyNode():
def __init__(self, val, cnt):
self.val = val
self.cnt = cnt
@ssshukla26
ssshukla26 / RegExMemoization.py
Created October 8, 2021 16:34
RegEx Matching Leetcode 10
# Leetcode : https://leetcode.com/problems/regular-expression-matching/
# Reference : https://www.youtube.com/watch?v=HAA8mgxlov8
from functools import cache
class Solution:
def isMatch(self, s: str, p: str) -> bool:
a = len(s)
b = len(p)
@ssshukla26
ssshukla26 / MergeSort.py
Created October 3, 2021 03:43
Basic Merge Sort
from typing import List, Tuple
def merge_sort(arr: List):
# Sort and Merge
def merge(first: Tuple, second: Tuple) -> Tuple:
# Init
a, n = first
b, m = second
@ssshukla26
ssshukla26 / ExpressionTree.py
Last active September 23, 2021 01:35
Design an Expression Tree With Evaluate Function
from typing import List
class TreeNode(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
return