leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 8c9a3b0fc07b26573deb58c63cedc3f0653a8bc4
parent 4a255da271b67eeb8b8da3e0d37beeff2f148f33
Author: Andrew Laack <andrew@laack.co>
Date:   Wed,  2 Jul 2025 10:11:42 -0500

Completed https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/submissions/1683990217/

Diffstat:
Adesign-add-and-search-words-data-structure/design-add.py | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Afind-the-index-of-the-first-occurrence-in-a-string/find-the-iV1.py | 7+++++++
Apalindrome-number/palindrome-number.py | 10++++++++++
3 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/design-add-and-search-words-data-structure/design-add.py b/design-add-and-search-words-data-structure/design-add.py @@ -0,0 +1,54 @@ +class TreeNode(): + def __init__(self): + self.isWord = False + self.chars = {} + +class WordDictionary: + + def __init__(self): + self.root = TreeNode() + + def addWord(self, word: str) -> None: + current = self.root + + for char in word: + if char in current.chars: + current = current.chars[char] + else: + node = TreeNode() + current.chars[char] = node + current = current.chars[char] + + current.isWord = True + + def search(self, word: str, root = None) -> bool: + + current = None + + if root is None: + current = self.root + else: + current = root + + for index, char in enumerate(word): + + if char == '.': + for option in current.chars: + test = self.search(word[index + 1:], current.chars[option]) + if test: + return True + return False + + if char in current.chars: + current = current.chars[char] + continue + else: + return False + + return current.isWord + + +# Your WordDictionary object will be instantiated and called as such: +# obj = WordDictionary() +# obj.addWord(word) +# param_2 = obj.search(word) diff --git a/find-the-index-of-the-first-occurrence-in-a-string/find-the-iV1.py b/find-the-index-of-the-first-occurrence-in-a-string/find-the-iV1.py @@ -0,0 +1,7 @@ +class Solution: + def strStr(self, haystack: str, needle: str) -> int: + for i in range(0, 1 + len(haystack) - len(needle)): + if haystack[i:i+len(needle)] == needle: + return i + return -1 + diff --git a/palindrome-number/palindrome-number.py b/palindrome-number/palindrome-number.py @@ -0,0 +1,10 @@ +class Solution: + def isPalindrome(self, x: int) -> bool: + + ls = list(str(x)) + ls_cp = ls.copy() + ls_cp.reverse() + + if ls == ls_cp: + return True + return False