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,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 lowercase
s = s.lower()

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

# Determine if it 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,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,21 @@
from typing import List, Union, Collection, Mapping, Optional

class Solution:
def minSwaps(self, data: List[int]) -> int:

# Window size
k = sum(data)

answer = val = 0

for i, v in enumerate(data):

val += v

if i >= k:
val -= data[i - k]

if i >= k - 1:
answer = max(answer, val)

return k - answer
16 changes: 16 additions & 0 deletions src/my_project/interviews/top_150_questions_round_21/two_sum.py
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 -1
18 changes: 18 additions & 0 deletions tests/test_150_questions_round_21/test_two_sum_round_21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
from src.my_project.interviews.top_150_questions_round_21\
.two_sum import Solution

class TwoSumTestCase(unittest.TestCase):

def test_is_two_sum(self):
solution = Solution()
output = solution.twoSum(nums=[2,7,11,15], target=9)
target = [0,1]
for k, v in enumerate(target):
self.assertEqual(v, output[k])

def test_is_no_two_sum(self):
solution = Solution()
output = solution.twoSum(nums=[2,7,11,15], target=0)
target = -1
self.assertEqual(output, target)