arrays and hashing Flashcards

1
Q

contains duplicate
https://leetcode.com/problems/contains-duplicate/description/

A

make hashset
iterate through input array
if number is in the hashset return true
otherwise add number to hashset

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

two sum
https://leetcode.com/problems/two-sum/

A

prevmap = {}

    for i, n in enumerate(nums):
        dif = target - n
        if  dif in prevmap:
            return [prevmap[dif], i]
        prevmap[n] = i
    return None
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

valid anagram
https://leetcode.com/problems/valid-anagram/

A

if len(s) != len(t):
return False

    countS, countT = {}, {}

    for i in range(len(s)):
        countS[s[i]] = 1 + countS.get(s[i], 0)
        countT[t[i]] = 1 + countT.get(t[i], 0)

    return countS == countT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

is subsequence
https://leetcode.com/problems/is-subsequence/description/

A

i, j = 0, 0

    while i < len(s) and j < len(t):
        if s[i] == t[j]:
            i += 1
        j += 1
    if i == len(s):
        return True
    else:
        return False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

length of last word
https://leetcode.com/problems/length-of-last-word/

A

i, length = len(s) - 1, 0

    while s[i] == " ":
        i -= 1
    while i >= 0 and s[i] != " ":
        length += 1
        i -= 1
    return length
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

longest common prefix
https://leetcode.com/problems/longest-common-prefix/submissions/

A

res = “”
for i in range(len(strs[0])):
for s in strs:
if i == len(s) or s[i] != strs[0][i]:
return res
res += strs[0][i]
return res

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

pascal’s triangle
https://leetcode.com/problems/pascals-triangle/submissions/

A

def generate(self, rowIndex) -> List[List[int]]:
if rowIndex == 0:
return [[1]]
else:
return self.getAllRow(rowIndex - 1)

def getAllRow(self, rowIndex):
    if rowIndex == 0:
        return [[1]]
    ListPrec = self.getAllRow(rowIndex - 1)
    Len = len(ListPrec[-1])
    ListPrec.append([1])
    for i in range(0, Len - 1):
        ListPrec[-1].append(ListPrec[-2][i] + ListPrec[-2][i + 1])
    ListPrec[-1].append(1)
    return ListPrec
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

remove element
https://leetcode.com/problems/remove-element/description/

A

k = 0

    for i in range(len(nums)):
        if nums[i] != val:
            nums[k] = nums[i]
            k += 1
    
    return k
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

unique email address
https://leetcode.com/problems/unique-email-addresses/description/

A

unique_emails: set[str] = set()
for email in emails:
local_name, domain_name = email.split(‘@’)
local_name = local_name.split(‘+’)[0]
local_name = local_name.replace(‘.’, ‘’)
email = local_name + ‘@’ + domain_name
unique_emails.add(email)
return len(unique_emails)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

isomorphic strings
https://leetcode.com/problems/isomorphic-strings/description/

A

mapST, mapTS = {}, {}

    for c1, c2 in zip(s, t):
        if (c1 in mapST and mapST[c1] != c2) or (c2 in mapTS and mapTS[c2] != c1):
            return False
        mapST[c1] = c2
        mapTS[c2] = c1

    return True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

remove duplicate from sorted array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/

A

l = 1

    for r in range(1, len(nums)):
        if nums[r] != nums[r - 1]:
            nums[l] = nums[r]
            l += 1

    return l
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

concatenation of array
https://leetcode.com/problems/concatenation-of-array/description/

A

ans = []

    for i in range(2):
        for n in nums:
            ans.append(n)

    return ans
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

minimum stack
https://neetcode.io/problems/minimum-stack

A

def __init__(self):
self.stack = []
self.minStack = []

def push(self, val: int) -> None:
    self.stack.append(val)
    val = min(val, self.minStack[-1] if self.minStack else val)
    self.minStack.append(val)

def pop(self) -> None:
    self.stack.pop()
    self.minStack.pop()

def top(self) -> int:
    return self.stack[-1]

def getMin(self) -> int:
    return self.minStack[-1]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

valid parentheses
https://leetcode.com/problems/valid-parentheses/description/

A

stack = []
closeToOpen = {“)” : “(“, “]” : “[”, “}” : “{“}

    for c in s: 
        if c in closeToOpen:
            if stack and stack[-1] == closeToOpen[c]:
                stack.pop()
            else:
                return False
        else:
            stack.append(c)
    
    return True if not stack else False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly