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,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 []





Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class WordDictionary {
private root: Map<string, any>;

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<string, any>, 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);
}
}
Original file line number Diff line number Diff line change
@@ -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.."))