Skip to content

Commit defbe0c

Browse files
authored
Merge pull request #1566 from ivanpenaloza/march04
adding algo
2 parents 3cc0667 + 14434d2 commit defbe0c

5 files changed

Lines changed: 188 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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import deque, defaultdict
4+
5+
class TrieNode:
6+
def __init__(self):
7+
self.children = {} # Dictionary to store child nodes
8+
self.is_end_of_word = False # Flag to mark end of a word
9+
10+
class Trie:
11+
12+
def __init__(self):
13+
self.root = TrieNode()
14+
15+
def insert(self, word: str) -> None:
16+
node = self.root
17+
for char in word:
18+
if char not in node.children:
19+
node.children[char] = TrieNode()
20+
node = node.children[char]
21+
node.is_end_of_word = True
22+
23+
def search(self, word: str) -> bool:
24+
node = self.root
25+
for char in word:
26+
if char not in node.children:
27+
return False
28+
node = node.children[char]
29+
return node.is_end_of_word
30+
31+
def startsWith(self, prefix: str) -> bool:
32+
node = self.root
33+
for char in prefix:
34+
if char not in node.children:
35+
return False
36+
node = node.children[char]
37+
return True
38+
39+
40+
41+
# Your Trie object will be instantiated and called as such:
42+
# obj = Trie()
43+
# obj.insert(word)
44+
# param_2 = obj.search(word)
45+
# param_3 = obj.startsWith(prefix)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class TrieNode {
2+
children: Map<string, TrieNode>;
3+
isEndOfWord: boolean;
4+
5+
constructor() {
6+
this.children = new Map<string, TrieNode>();
7+
this.isEndOfWord = false;
8+
}
9+
}
10+
11+
class Trie {
12+
private root: TrieNode;
13+
14+
constructor() {
15+
this.root = new TrieNode();
16+
}
17+
18+
insert(word: string): void {
19+
let node = this.root;
20+
for (const char of word) {
21+
if (!node.children.has(char)) {
22+
node.children.set(char, new TrieNode());
23+
}
24+
node = node.children.get(char)!;
25+
}
26+
node.isEndOfWord = true;
27+
}
28+
29+
search(word: string): boolean {
30+
let node = this.root;
31+
for (const char of word) {
32+
if (!node.children.has(char)) {
33+
return false;
34+
}
35+
node = node.children.get(char)!;
36+
}
37+
return node.isEndOfWord;
38+
}
39+
40+
startsWith(prefix: string): boolean {
41+
let node = this.root;
42+
for (const char of prefix) {
43+
if (!node.children.has(char)) {
44+
return false;
45+
}
46+
node = node.children.get(char)!;
47+
}
48+
return true;
49+
}
50+
}
51+
52+
/**
53+
* Your Trie object will be instantiated and called as such:
54+
* var obj = new Trie()
55+
* obj.insert(word)
56+
* var param_2 = obj.search(word)
57+
* var param_3 = obj.startsWith(prefix)
58+
*/
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_93_implement_trie import Trie, TrieNode
5+
6+
7+
class ImplementTrieTestCase(unittest.TestCase):
8+
9+
def test_example_1(self):
10+
"""
11+
Input: ["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
12+
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
13+
Output: [null, null, true, false, true, null, true]
14+
15+
Explanation:
16+
Trie trie = new Trie();
17+
trie.insert("apple");
18+
trie.search("apple"); // return True
19+
trie.search("app"); // return False
20+
trie.startsWith("app"); // return True
21+
trie.insert("app");
22+
trie.search("app"); // return True
23+
"""
24+
trie = Trie()
25+
26+
# Insert "apple"
27+
trie.insert("apple")
28+
29+
# Search for "apple" - should return True
30+
self.assertTrue(trie.search("apple"))
31+
32+
# Search for "app" - should return False (prefix exists but not complete word)
33+
self.assertFalse(trie.search("app"))
34+
35+
# Check if "app" is a prefix - should return True
36+
self.assertTrue(trie.startsWith("app"))
37+
38+
# Insert "app"
39+
trie.insert("app")
40+
41+
# Search for "app" again - should now return True
42+
self.assertTrue(trie.search("app"))

0 commit comments

Comments
 (0)