Skip to content

Instantly share code, notes, and snippets.

View amarjitdhillon's full-sized avatar
🎯
Focusing

Amarjit Singh Dhillon amarjitdhillon

🎯
Focusing
View GitHub Profile
@amarjitdhillon
amarjitdhillon / letter-combinations-of-a-phone-number.py
Created February 8, 2022 09:12
Letter Combinations of a Phone Number
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
result = []
keypad = {2: 'abc', 3: 'def', 4: 'ghi', 5: 'jkl', 6: 'mno', 7: 'pqrs', 8: 'tuv', 9: 'wxyz'}
# edge case
if len(digits) == 0:
return []
def dfsKeypad(index,path):
@amarjitdhillon
amarjitdhillon / reorder-data-in-log-files.py
Created February 8, 2022 20:26
Reorder Data in Log Files
class Solution:
def reorderLogFiles(self, logs: List[str]) -> List[str]:
result, plogs = [], []
for i, log in enumerate(logs):
lg = log.split(" ") # split the string over the space and convert to a list.
if lg[1].isalpha(): # Identify the type of log as letter log or digit log : this can be checked if 2nd element is string or a digit?
# tuple with 4 keys (k1,k2,k3,k4)
# k1 for which type of logs, k2 for contents of logs, k3 for log identifier and k4 for original index in logs list
@amarjitdhillon
amarjitdhillon / flip-string-to-monotone-increasing.py
Last active February 8, 2022 22:56
Flip String to Monotone Increasing
class Solution:
def minFlipsMonoIncr(self, s: str) -> int:
oneCount, flipCount = 0,0 # intially both are 0
for c in s:
if c == "1":
oneCount += 1
else: # if not 1 then it's a zero
if oneCount >= 1:
flipCount += 1 # in case we have already seen a zero, we will increase the flipCount
flipCount = min(flipCount, oneCount) # we will do fip if it's count is less then # of 1's seen so far
@amarjitdhillon
amarjitdhillon / word-search.py
Created February 9, 2022 09:35
Word Search
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
R = len(board) # num of rows
C = len(board[0]) # num of cols
# use a set to hold all the values which are visited before in the path
visited = set()
'''
Desc for findIfExists function
@amarjitdhillon
amarjitdhillon / count-binary-substrings.py
Created February 9, 2022 21:30
Count Binary Substrings
class Solution:
def countBinarySubstrings(self, s: str) -> int:
'''
Intuition: We can convert the string s into array groups that represent the length of same-character contiguous blocks within the string. For example, if s = "110001111000000", then groups lengths will be [2, 3, 4, 6]. Then, we can make min(groups[i], groups[i+1]) valid binary strings within this string. Because the binary digits to the left or right of this string must change at the boundary, our answer can never be larger.
Let's understand this with an example
For 00011 we have 2 groups of lengths 3 and 2, yet here we can make 2 valid substrings which are min(4,2).
These strings are 0011 and 01. So clearly min group is the limiting factor
For 000011, we have 2 groups of lengths 4 and 2, yet here we can make 2 valid substrings which are min(4,2).
These strings are 0011 and 01. I hope the logic is clear
@amarjitdhillon
amarjitdhillon / word-pattern.py
Created February 10, 2022 00:10
Word Pattern
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
hm = {}
# we need to split the s over the space(" ")
s = s.split(" ")
# if the lengths of pattern and s are not same then return False
if len(s) != len(pattern):
return False
@amarjitdhillon
amarjitdhillon / design-parking-system.py
Created February 10, 2022 17:10
Design Parking System
class ParkingSystem:
# initialize the parking object
def __init__(self, big: int, medium: int, small: int):
# to hold various types of parking we are using a dict in constructor
self.lot = {}
# add the parking counters in this lot dict
self.lot[1] = big
self.lot[2] = medium
self.lot[3] = small
@amarjitdhillon
amarjitdhillon / analyze-user-website-visit-pattern.py
Created February 10, 2022 23:45
Analyze User Website Visit Pattern
class Solution:
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
# zip the time, user and website to one list
tuw = list(zip(timestamp,username,website))
# we need to sort them by time --> username --> website
sorted_tuw = sorted(tuw)
# We will polulate a user history hashmap of various pages visited. The hashmap is of type {user: [pages]}
@amarjitdhillon
amarjitdhillon / the-kth-factor-of-n.py
Created February 11, 2022 01:23
The kth Factor of n
class Solution:
def kthFactor(self, n: int, k: int) -> int:
# counter represent the i^th factor of the number found so far
counter = 0
# go over the number from 1 to n as we need to divide the n by this number to find if that number is a factor
for i in range(1, n+1):
if n%i == 0:
@amarjitdhillon
amarjitdhillon / merge-intervals.py
Created February 11, 2022 07:46
Merge Intervals
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
# sort the intervals by the start value, in case they are not sorted by default
intervals.sort(key = lambda x: x[0])
# edge case. If num elements is one, then return the same list
if len(intervals) == 1:
return intervals