From 14434d29310228a4690dddfcf7063876a9ef76f6 Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 4 Mar 2026 04:34:01 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_3.py | 21 +++++++ .../common_algos/valid_palindrome_round_3.py | 22 +++++++ .../ex_93_implement_trie.py | 45 ++++++++++++++ .../ex_93_implement_trie.ts | 58 +++++++++++++++++++ .../test_93_implement_trie_round_22.py | 42 ++++++++++++++ 5 files changed, 188 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_93_implement_trie.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_93_implement_trie.ts create mode 100644 tests/test_150_questions_round_22/test_93_implement_trie_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py new file mode 100644 index 00000000..14d98152 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py @@ -0,0 +1,21 @@ +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_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py new file mode 100644 index 00000000..667a159e --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.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_93_implement_trie.py b/src/my_project/interviews/top_150_questions_round_22/ex_93_implement_trie.py new file mode 100644 index 00000000..77670802 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_93_implement_trie.py @@ -0,0 +1,45 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque, defaultdict + +class TrieNode: + def __init__(self): + self.children = {} # Dictionary to store child nodes + self.is_end_of_word = False # Flag to mark end of a word + +class Trie: + + def __init__(self): + self.root = TrieNode() + + def insert(self, word: str) -> None: + node = self.root + for char in word: + if char not in node.children: + node.children[char] = TrieNode() + node = node.children[char] + node.is_end_of_word = True + + def search(self, word: str) -> bool: + node = self.root + for char in word: + if char not in node.children: + return False + node = node.children[char] + return node.is_end_of_word + + def startsWith(self, prefix: str) -> bool: + node = self.root + for char in prefix: + if char not in node.children: + return False + node = node.children[char] + return True + + + +# Your Trie object will be instantiated and called as such: +# obj = Trie() +# obj.insert(word) +# param_2 = obj.search(word) +# param_3 = obj.startsWith(prefix) \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_93_implement_trie.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_93_implement_trie.ts new file mode 100644 index 00000000..fa1a606d --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_93_implement_trie.ts @@ -0,0 +1,58 @@ +class TrieNode { + children: Map; + isEndOfWord: boolean; + + constructor() { + this.children = new Map(); + this.isEndOfWord = false; + } +} + +class Trie { + private root: TrieNode; + + constructor() { + this.root = new TrieNode(); + } + + insert(word: string): void { + let node = this.root; + for (const char of word) { + if (!node.children.has(char)) { + node.children.set(char, new TrieNode()); + } + node = node.children.get(char)!; + } + node.isEndOfWord = true; + } + + search(word: string): boolean { + let node = this.root; + for (const char of word) { + if (!node.children.has(char)) { + return false; + } + node = node.children.get(char)!; + } + return node.isEndOfWord; + } + + startsWith(prefix: string): boolean { + let node = this.root; + for (const char of prefix) { + if (!node.children.has(char)) { + return false; + } + node = node.children.get(char)!; + } + return true; + } +} + +/** + * Your Trie object will be instantiated and called as such: + * var obj = new Trie() + * obj.insert(word) + * var param_2 = obj.search(word) + * var param_3 = obj.startsWith(prefix) + */ \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_93_implement_trie_round_22.py b/tests/test_150_questions_round_22/test_93_implement_trie_round_22.py new file mode 100644 index 00000000..2808b0a6 --- /dev/null +++ b/tests/test_150_questions_round_22/test_93_implement_trie_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_93_implement_trie import Trie, TrieNode + + +class ImplementTrieTestCase(unittest.TestCase): + + def test_example_1(self): + """ + Input: ["Trie", "insert", "search", "search", "startsWith", "insert", "search"] + [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]] + Output: [null, null, true, false, true, null, true] + + Explanation: + Trie trie = new Trie(); + trie.insert("apple"); + trie.search("apple"); // return True + trie.search("app"); // return False + trie.startsWith("app"); // return True + trie.insert("app"); + trie.search("app"); // return True + """ + trie = Trie() + + # Insert "apple" + trie.insert("apple") + + # Search for "apple" - should return True + self.assertTrue(trie.search("apple")) + + # Search for "app" - should return False (prefix exists but not complete word) + self.assertFalse(trie.search("app")) + + # Check if "app" is a prefix - should return True + self.assertTrue(trie.startsWith("app")) + + # Insert "app" + trie.insert("app") + + # Search for "app" again - should now return True + self.assertTrue(trie.search("app")) \ No newline at end of file