[英]Find a prefix based on trie search | | python
問題:給定一個business_names(字符串)列表和一個searchTerm(字符串)。 返回一個business_names 列表,其中包含searchTerm 作為business_names 中的前綴。
Example 1.
Input:
business_names[] = { "burger king", "McDonald's", "super duper burger's", "subway", "pizza hut"}
searchTerm = "bur"
Ouput:
["burger king", "super duper burger's"]
我試過用下面的方法解決。
但我想實施 trie 方法來解決這個問題。 有人請在這里幫忙嗎? https://www.geeksforgeeks.org/trie-insert-and-search/
任何要解決的線性解決方案
def prefix(business_names, searchTerm):
split = [i.split() for i in business_names]
ans = []
for name in split:
for i in range(len(name)):
query = ' '.join(name[i:])
if query.startswith(searchTerm):
ans.append(name)
break
return [' '.join(i) for i in ans]
我不知道trie approach
是什么意思,如果這是你需要的,但我會寫得更簡單——沒有join
、 range
、 len
為了確保我也使用lower()
business_names = ["burger king", "McDonald's", "super duper burger's", "subway", "pizza hut"]
searchTerm = "bur"
def prefix(business_names, searchTerm):
searchTerm = searchTerm.lower()
results = []
for name in business_names:
for word in name.split(' '):
word = word.lower()
if word.startswith(searchTerm):
results.append(name)
break
return results
print(prefix(business_names, searchTerm))
編輯:
我從鏈接中獲取代碼並創建此代碼。
但我必須改變兩件事。
它只適用於字母az
所以我必須刪除'
並轉換為lower()
它只搜索完整的單詞,但在刪除and pCrawl.isEndOfWord
並return pCrawl.= None and pCrawl.isEndOfWord
,它似乎找到了使用searchTerm
統計的單詞
但我有一個疑問:也許它可以比O(n^2)
搜索得更好,但首先它必須構建Trie
,而且還需要一些時間。 因此,當您總是在相同的文本中搜索並且您只需要構建一次Trie
時,它會很有用。 但是您必須為每個企業名稱構建單獨的Trie
- 而且它不必更快。
.
class TrieNode:
# Trie node class
def __init__(self):
self.children = [None]*26
# isEndOfWord is True if node represent the end of the word
self.isEndOfWord = False
class Trie:
# Trie data structure class
def __init__(self):
self.root = self.getNode()
def getNode(self):
# Returns new trie node (initialized to NULLs)
return TrieNode()
def _charToIndex(self,ch):
# private helper function
# Converts key current character into index
# use only 'a' through 'z' and lower case
return ord(ch)-ord('a')
def insert(self, key):
# If not present, inserts key into trie
# If the key is prefix of trie node,
# just marks leaf node
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
# if current character is not present
if not pCrawl.children[index]:
pCrawl.children[index] = self.getNode()
pCrawl = pCrawl.children[index]
# mark last node as leaf
pCrawl.isEndOfWord = True
def search(self, key):
# Search key in the trie
# Returns true if key presents
# in trie, else false
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
return False
pCrawl = pCrawl.children[index]
return pCrawl != None #and pCrawl.isEndOfWord # <-- check `isEndOfWord` to search full words
# driver function
def prefix(business_names, searchTerm):
searchTerm = searchTerm.lower()
results = []
for name in business_names:
# Input keys (use only 'a' through 'z' and lower case)
# remove `'` and convert to list with lower case words
keys = name.lower().replace("'", "").split(" ")
#print('keys:', keys)
# Trie object
t = Trie()
# Construct trie
for key in keys:
#print('key:', key)
t.insert(key)
# Search in trie
if t.search(searchTerm) is True:
results.append(name)
return results
if __name__ == '__main__':
business_names = ["burger king", "McDonald's", "super duper burger's", "subway", "pizza hut"]
searchTerm = "bur"
results = prefix(business_names, searchTerm)
print( results )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.