This file contains hidden or 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 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): |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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]} |
This file contains hidden or 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 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: |
This file contains hidden or 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 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 | |