Skip to content

Instantly share code, notes, and snippets.

View avin-sharma's full-sized avatar

Avin Sharma avin-sharma

View GitHub Profile
class Solution:
def numDecodings(self, s: str) -> int:
# 2216 -> 2 2 1 6, 2 21 6, 22 1 6, 2 2 16, 22 16
# 221 -> 2 2 1, 2 21, 22 1
# 22 -> 2 2, 22
if s[0] == '0':
return 0
decodes = [0 for _ in range(len(s) + 1)]
decodes[0] = 1
@avin-sharma
avin-sharma / solution.py
Created March 15, 2020 23:49
32. Longest Valid Parentheses
class Solution:
def longestValidParentheses(self, s: str) -> int:
if not s:
return 0
isValid = [0 for _ in range(len(s))]
for i in range(len(s)):
if s[i] == ')':
if i > 0 and s[i-1] == '(':
@avin-sharma
avin-sharma / solution.py
Created March 15, 2020 20:17
5. Longest Palindromic Substring
class Solution:
def longestPalindrome(self, s: str) -> str:
longest = 0
p = ''
n = len(s)
for i in range(len(s)):
length = self.isPalindrome(i, i, s, n)
if length > longest:
longest = length
@avin-sharma
avin-sharma / solution.py
Created March 15, 2020 19:32
114. Flatten Binary Tree to Linked List
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def flatten(self, root: TreeNode, right:TreeNode = None) -> None:
"""
@avin-sharma
avin-sharma / sub_array_sum.py
Created March 15, 2020 14:34
560. Subarray Sum Equals K
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
count = 0
cumulative = {0:1}
total = 0
for i in range(len(nums)):
total += nums[i]
if total - k in cumulative:
count += cumulative[total - k]
@avin-sharma
avin-sharma / board.py
Created April 21, 2019 16:35
A simple terminal based 2 player TIC-TAC-TOE
# Terminal TIC-TAC-TOE
# Written in Python 3
class Board:
def __init__(self, n=3):
self.n = n
self.state = [[' ' for _ in range(n)] for _ in range(n)]
self.active_player = 'X'
self.t = 0
@avin-sharma
avin-sharma / solution.py
Created July 30, 2017 13:07
Heaps: Find the Running Median
# https://www.hackerrank.com/challenges/ctci-find-the-running-median/problem
import sys
minHeap = []
maxHeap = []
minSize = 0
maxSize = 0
def getParentIndex(index):
@avin-sharma
avin-sharma / solution.py
Created July 21, 2017 07:56
Queues: A Tale of Two Stack
# https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks
class MyQueue(object):
def __init__(self):
self.pushStack = []
self.popStack = []
def peek(self):
if not self.popStack:
while self.pushStack:
@avin-sharma
avin-sharma / balancedBrackets.py
Created July 20, 2017 22:16
Stacks: Balanced Brackets
# https://www.hackerrank.com/challenges/ctci-balanced-brackets
def is_matched(expression):
open_brackets = '{[('
stack = []
d = {'}':'{', ')':'(', ']':'['}
for c in expression:
if c in open_brackets:
stack.append(c)
elif len(stack) == 0:
@avin-sharma
avin-sharma / detectCycle.py
Created July 18, 2017 21:56
Linked Lists: Detect a Cycle
# https://www.hackerrank.com/challenges/ctci-linked-list-cycle/problem
"""
Detect a cycle in a linked list. Note that the head pointer may be 'None' if the list is empty.
A Node is defined as:
class Node(object):
def __init__(self, data = None, next_node = None):
self.data = data