Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:

answer = dict()

for k, v in enumerate(nums):

if v in answer:
return [answer[v], k]
else:
answer[target - v] = k

return []
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
import re

class Solution:
def isPalindrome(self, s: str) -> bool:

# To lower
s = s.lower()

# Remove non-alphanumeric characters
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)

# Determine if s is palindrome or not

len_s = len(s)

for i in range(len_s//2):

if s[i] != s[len_s - 1 - i]:
return False

return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List, Union, Collection, Mapping, Optional

class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:

# Make a grid of 0's with len(text2) + 1 columns
# and len(text1) + 1 rows.
len_1 = len(text1)
len_2 = len(text2)
dp_grid = [[0]*(len_2+1) for _ in range(len_1+1)]

# Iterate up each column, starting from the last one.
for j in reversed(range(len_2)):
for i in reversed(range(len_1)):
if text1[i] == text2[j]:
dp_grid[i][j] = dp_grid[i+1][j+1] + 1
else:
dp_grid[i][j] = max(dp_grid[i+1][j], dp_grid[i][j+1])

# The original problem's answer is in dp_grid[0][0]. Return it.
return dp_grid[0][0]

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import List, Union, Collection, Mapping, Optional

# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution:
def lowestCommonAncestor(self, root: TreeNode, nodes: List[TreeNode]) -> 'TreeNode':

node_set = set(nodes)

def dfs(node: TreeNode):

if not node or node in node_set:
return node

left = dfs(node.left)
right = dfs(node.right)

if left and right:
return node

return left if left else right

return dfs(root)

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def singleNumber(self, nums: List[int]) -> int:

answer = 0

for num in nums:
answer ^= num

return answer
11 changes: 11 additions & 0 deletions tests/test_150_questions_round_21/test_single_number_round_21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unittest
from src.my_project.interviews.top_150_questions_round_21\
.single_number import Solution

class SingleNumberTestCase(unittest.TestCase):

def test_single_number(self):
solution = Solution()
output = solution.singleNumber(nums=[2,2,1])
target = 1
self.assertEqual(output, target)