diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py new file mode 100644 index 00000000..6c0c6330 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py @@ -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 [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py new file mode 100644 index 00000000..cad2b1e6 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py @@ -0,0 +1,22 @@ +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=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 \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_73_path_sum.py b/src/my_project/interviews/top_150_questions_round_22/ex_73_path_sum.py new file mode 100644 index 00000000..449b0fe7 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_73_path_sum.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + + def hasPathSum(self, root: TreeNode, targetSum: int) -> bool: + + if not root: + return False + if not root.left and not root.right and root.val == targetSum: + return True + else: + temp_target = targetSum - root.val + return self.hasPathSum(root.left, temp_target) \ + or self.hasPathSum(root.right, temp_target) diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_73_path_sum.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_73_path_sum.ts new file mode 100644 index 00000000..d0d3758c --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_73_path_sum.ts @@ -0,0 +1,12 @@ +import { TreeNode } from './TreeNode'; + +export function hasPathSum(root: TreeNode | null, targetSum: number): boolean { + if (!root) { + return false; + } + if (!root.left && !root.right && root.val === targetSum) { + return true; + } + const tempTarget = targetSum - root.val; + return hasPathSum(root.left, tempTarget) || hasPathSum(root.right, tempTarget); +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_73_path_sum_round_22.py b/tests/test_150_questions_round_22/test_73_path_sum_round_22.py new file mode 100644 index 00000000..5efc6120 --- /dev/null +++ b/tests/test_150_questions_round_22/test_73_path_sum_round_22.py @@ -0,0 +1,19 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_73_path_sum import Solution, TreeNode + + +class HasPathSumTestCase(unittest.TestCase): + + def test_is_path_sum(self): + solution = Solution() + tree = TreeNode(1, TreeNode(2), TreeNode(3)) + output = solution.hasPathSum(root=tree, targetSum=3) + self.assertTrue(output) + + def test_is_no_path_sum(self): + solution = Solution() + tree = TreeNode(1, TreeNode(2), TreeNode(3)) + output = solution.hasPathSum(root=tree, targetSum=10) + self.assertFalse(output) \ No newline at end of file