Skip to content

Commit 95e9fb8

Browse files
authored
Merge pull request #1568 from ivanpenaloza/march06
adding algo
2 parents 7eb623d + 5a1f3b8 commit 95e9fb8

5 files changed

Lines changed: 174 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
17+
18+
19+
20+
21+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import deque, defaultdict
4+
5+
class WordDictionary:
6+
7+
def __init__(self):
8+
self.root = {}
9+
10+
11+
def addWord(self, word: str) -> None:
12+
node = self.root
13+
for char in word:
14+
if char not in node:
15+
node[char] = {}
16+
node = node[char]
17+
node['$'] = True # Mark end of word
18+
19+
20+
def search(self, word: str) -> bool:
21+
def dfs(node, i):
22+
if i == len(word):
23+
return '$' in node
24+
25+
char = word[i]
26+
if char == '.':
27+
# Wildcard: try all possible characters at this position
28+
for key in node:
29+
if key != '$' and dfs(node[key], i + 1):
30+
return True
31+
return False
32+
else:
33+
# Exact character match
34+
if char not in node:
35+
return False
36+
return dfs(node[char], i + 1)
37+
38+
return dfs(self.root, 0)
39+
40+
41+
# Your WordDictionary object will be instantiated and called as such:
42+
# obj = WordDictionary()
43+
# obj.addWord(word)
44+
# param_2 = obj.search(word)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class WordDictionary {
2+
private root: Map<string, any>;
3+
4+
constructor() {
5+
this.root = new Map();
6+
}
7+
8+
addWord(word: string): void {
9+
let node = this.root;
10+
for (const char of word) {
11+
if (!node.has(char)) {
12+
node.set(char, new Map());
13+
}
14+
node = node.get(char);
15+
}
16+
node.set('$', true); // Mark end of word
17+
}
18+
19+
search(word: string): boolean {
20+
const dfs = (node: Map<string, any>, i: number): boolean => {
21+
if (i === word.length) {
22+
return node.has('$');
23+
}
24+
25+
const char = word[i];
26+
if (char === '.') {
27+
// Wildcard: try all possible characters at this position
28+
for (const [key, childNode] of node.entries()) {
29+
if (key !== '$' && dfs(childNode, i + 1)) {
30+
return true;
31+
}
32+
}
33+
return false;
34+
} else {
35+
// Exact character match
36+
if (!node.has(char)) {
37+
return false;
38+
}
39+
return dfs(node.get(char), i + 1);
40+
}
41+
};
42+
43+
return dfs(this.root, 0);
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import unittest
2+
from typing import Optional, List
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.ex_94_design_add_and_search_words_data_structure import WordDictionary
5+
6+
7+
class DesignAddAndSearchWordsDataStructureTestCase(unittest.TestCase):
8+
9+
def test_example_1(self):
10+
"""
11+
Input: ["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
12+
[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
13+
Output: [null,null,null,null,false,true,true,true]
14+
15+
Explanation:
16+
WordDictionary wordDictionary = new WordDictionary();
17+
wordDictionary.addWord("bad");
18+
wordDictionary.addWord("dad");
19+
wordDictionary.addWord("mad");
20+
wordDictionary.search("pad"); // return False
21+
wordDictionary.search("bad"); // return True
22+
wordDictionary.search(".ad"); // return True
23+
wordDictionary.search("b.."); // return True
24+
"""
25+
wordDictionary = WordDictionary()
26+
27+
# Add words
28+
wordDictionary.addWord("bad")
29+
wordDictionary.addWord("dad")
30+
wordDictionary.addWord("mad")
31+
32+
# Search for "pad" - should return False
33+
self.assertFalse(wordDictionary.search("pad"))
34+
35+
# Search for "bad" - should return True
36+
self.assertTrue(wordDictionary.search("bad"))
37+
38+
# Search for ".ad" - should return True (matches bad, dad, mad)
39+
self.assertTrue(wordDictionary.search(".ad"))
40+
41+
# Search for "b.." - should return True (matches bad)
42+
self.assertTrue(wordDictionary.search("b.."))

0 commit comments

Comments
 (0)