From e06e30ed1080f37f60a8272712c168907da6f7e8 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 8 Mar 2026 04:39:18 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_7.py | 22 ++++++++++ .../common_algos/valid_palindrome_round_7.py | 24 +++++++++++ ...etter_of_combinations_of_a_phone_number.py | 39 +++++++++++++++++ ...etter_of_combinations_of_a_phone_number.ts | 38 +++++++++++++++++ ...combinations_of_a_phone_number_round_22.py | 42 +++++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_96_letter_of_combinations_of_a_phone_number.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_96_letter_of_combinations_of_a_phone_number.ts create mode 100644 tests/test_150_questions_round_22/test_96_letter_of_combinations_of_a_phone_number_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py new file mode 100644 index 00000000..283dbf68 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py @@ -0,0 +1,22 @@ +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 [] + + + + + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py new file mode 100644 index 00000000..d7db84c7 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py @@ -0,0 +1,24 @@ +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 + diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_96_letter_of_combinations_of_a_phone_number.py b/src/my_project/interviews/top_150_questions_round_22/ex_96_letter_of_combinations_of_a_phone_number.py new file mode 100644 index 00000000..8c5a8f95 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_96_letter_of_combinations_of_a_phone_number.py @@ -0,0 +1,39 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque, defaultdict + +class Solution: + def letterCombinations(self, digits: str) -> List[str]: + if not digits: + return [] + + # Mapping of digits to letters + phone_map = { + '2': 'abc', + '3': 'def', + '4': 'ghi', + '5': 'jkl', + '6': 'mno', + '7': 'pqrs', + '8': 'tuv', + '9': 'wxyz' + } + + result = [] + + def backtrack(index: int, current: str): + # Base case: if we've processed all digits + if index == len(digits): + result.append(current) + return + + # Get the letters for the current digit + letters = phone_map[digits[index]] + + # Try each letter and recurse + for letter in letters: + backtrack(index + 1, current + letter) + + backtrack(0, "") + return result + \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_96_letter_of_combinations_of_a_phone_number.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_96_letter_of_combinations_of_a_phone_number.ts new file mode 100644 index 00000000..5ea93dec --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_96_letter_of_combinations_of_a_phone_number.ts @@ -0,0 +1,38 @@ +function letterCombinations(digits: string): string[] { + if (!digits || digits.length === 0) { + return []; + } + + // Mapping of digits to letters + const phoneMap: { [key: string]: string } = { + '2': 'abc', + '3': 'def', + '4': 'ghi', + '5': 'jkl', + '6': 'mno', + '7': 'pqrs', + '8': 'tuv', + '9': 'wxyz' + }; + + const result: string[] = []; + + function backtrack(index: number, current: string): void { + // Base case: if we've processed all digits + if (index === digits.length) { + result.push(current); + return; + } + + // Get the letters for the current digit + const letters = phoneMap[digits[index]]; + + // Try each letter and recurse + for (const letter of letters) { + backtrack(index + 1, current + letter); + } + } + + backtrack(0, ""); + return result; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_96_letter_of_combinations_of_a_phone_number_round_22.py b/tests/test_150_questions_round_22/test_96_letter_of_combinations_of_a_phone_number_round_22.py new file mode 100644 index 00000000..cfbac2bc --- /dev/null +++ b/tests/test_150_questions_round_22/test_96_letter_of_combinations_of_a_phone_number_round_22.py @@ -0,0 +1,42 @@ +import unittest +from typing import Optional, List +from src.my_project.interviews.top_150_questions_round_22\ +.ex_96_letter_of_combinations_of_a_phone_number import Solution + + +class LetterOfCombinationsOfAPhoneNumberTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + digits = "23" + expected = ["ad","ae","af","bd","be","bf","cd","ce","cf"] + result = self.solution.letterCombinations(digits) + self.assertEqual(sorted(result), sorted(expected)) + + def test_example_2(self): + digits = "2" + expected = ["a","b","c"] + result = self.solution.letterCombinations(digits) + self.assertEqual(sorted(result), sorted(expected)) + + def test_empty_string(self): + digits = "" + expected = [] + result = self.solution.letterCombinations(digits) + self.assertEqual(result, expected) + + def test_single_digit_9(self): + digits = "9" + expected = ["w", "x", "y", "z"] + result = self.solution.letterCombinations(digits) + self.assertEqual(sorted(result), sorted(expected)) + + def test_three_digits(self): + digits = "234" + # Should have 3 * 3 * 3 = 27 combinations + result = self.solution.letterCombinations(digits) + self.assertEqual(len(result), 27) + self.assertIn("adg", result) + self.assertIn("bfi", result) + self.assertIn("cfi", result) \ No newline at end of file