From 5a1f3b8dd056ac68c33569774bead49b07ba44f1 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 6 Mar 2026 04:43:21 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_5.py | 21 +++++++++ .../common_algos/valid_palindrome_round_5.py | 22 +++++++++ ...ign_add_and_search_words_data_structure.py | 44 ++++++++++++++++++ ...ign_add_and_search_words_data_structure.ts | 45 +++++++++++++++++++ ...nd_search_words_data_structure_round_22.py | 42 +++++++++++++++++ 5 files changed, 174 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_94_design_add_and_search_words_data_structure.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_94_design_add_and_search_words_data_structure.ts create mode 100644 tests/test_150_questions_round_22/test_94_design_add_and_search_words_data_structure_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py new file mode 100644 index 00000000..621aa5fb --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.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_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py new file mode 100644 index 00000000..c496472c --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.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_94_design_add_and_search_words_data_structure.py b/src/my_project/interviews/top_150_questions_round_22/ex_94_design_add_and_search_words_data_structure.py new file mode 100644 index 00000000..1af8e1bd --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_94_design_add_and_search_words_data_structure.py @@ -0,0 +1,44 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque, defaultdict + +class WordDictionary: + + def __init__(self): + self.root = {} + + + def addWord(self, word: str) -> None: + node = self.root + for char in word: + if char not in node: + node[char] = {} + node = node[char] + node['$'] = True # Mark end of word + + + def search(self, word: str) -> bool: + def dfs(node, i): + if i == len(word): + return '$' in node + + char = word[i] + if char == '.': + # Wildcard: try all possible characters at this position + for key in node: + if key != '$' and dfs(node[key], i + 1): + return True + return False + else: + # Exact character match + if char not in node: + return False + return dfs(node[char], i + 1) + + return dfs(self.root, 0) + + +# Your WordDictionary object will be instantiated and called as such: +# obj = WordDictionary() +# obj.addWord(word) +# param_2 = obj.search(word) \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_94_design_add_and_search_words_data_structure.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_94_design_add_and_search_words_data_structure.ts new file mode 100644 index 00000000..b46a8372 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_94_design_add_and_search_words_data_structure.ts @@ -0,0 +1,45 @@ +class WordDictionary { + private root: Map; + + constructor() { + this.root = new Map(); + } + + addWord(word: string): void { + let node = this.root; + for (const char of word) { + if (!node.has(char)) { + node.set(char, new Map()); + } + node = node.get(char); + } + node.set('$', true); // Mark end of word + } + + search(word: string): boolean { + const dfs = (node: Map, i: number): boolean => { + if (i === word.length) { + return node.has('$'); + } + + const char = word[i]; + if (char === '.') { + // Wildcard: try all possible characters at this position + for (const [key, childNode] of node.entries()) { + if (key !== '$' && dfs(childNode, i + 1)) { + return true; + } + } + return false; + } else { + // Exact character match + if (!node.has(char)) { + return false; + } + return dfs(node.get(char), i + 1); + } + }; + + return dfs(this.root, 0); + } +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_94_design_add_and_search_words_data_structure_round_22.py b/tests/test_150_questions_round_22/test_94_design_add_and_search_words_data_structure_round_22.py new file mode 100644 index 00000000..c1fe894a --- /dev/null +++ b/tests/test_150_questions_round_22/test_94_design_add_and_search_words_data_structure_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_94_design_add_and_search_words_data_structure import WordDictionary + + +class DesignAddAndSearchWordsDataStructureTestCase(unittest.TestCase): + + def test_example_1(self): + """ + Input: ["WordDictionary","addWord","addWord","addWord","search","search","search","search"] + [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]] + Output: [null,null,null,null,false,true,true,true] + + Explanation: + WordDictionary wordDictionary = new WordDictionary(); + wordDictionary.addWord("bad"); + wordDictionary.addWord("dad"); + wordDictionary.addWord("mad"); + wordDictionary.search("pad"); // return False + wordDictionary.search("bad"); // return True + wordDictionary.search(".ad"); // return True + wordDictionary.search("b.."); // return True + """ + wordDictionary = WordDictionary() + + # Add words + wordDictionary.addWord("bad") + wordDictionary.addWord("dad") + wordDictionary.addWord("mad") + + # Search for "pad" - should return False + self.assertFalse(wordDictionary.search("pad")) + + # Search for "bad" - should return True + self.assertTrue(wordDictionary.search("bad")) + + # Search for ".ad" - should return True (matches bad, dad, mad) + self.assertTrue(wordDictionary.search(".ad")) + + # Search for "b.." - should return True (matches bad) + self.assertTrue(wordDictionary.search("b..")) \ No newline at end of file